Does GCC assign any type to enums that can be specified in an assignment operator?

michael.a wild.eyed.boy.from.freecloud@gmail.com
Sat Jun 23 20:15:00 GMT 2007




> > Does GCC assign any type to enums that can be specified in an 
> assignment
> operator?
> 
> I'm not sure I understand the question.  I assume a C++ context, and not a
> C
> context (which has different rules regarding enum types).
> 
> What is the use case scenario?  Do you mean something like this...
> 
> - - - - -
> enum Foo { Uno, Dos, Tres };
> 
> class Bar
> {
>   int m;
> public:
>   Bar() : m(Uno) { }
>   Bar(Foo foo) : m(foo) { }
>   Bar(Bar const& bar) : m(bar.m) { }
>   Bar& operator = (Foo foo) { m = foo; return *this; }
>   operator Foo() const { return Foo(m); }
> };
> - - - - -
> 
> --Eljay
> 

I'm sorry, perhaps to be more specific, I was referring more so (if not
entirely) to anonymous enums (never really ever use tagged enums) 

Since asked, basicly imagine this:

 enum{ Uno, Dos, Tres }; //anonymous
 
 class Bar
 {
   int m;
 public:
   Bar& operator = (unsigned int foo) { m = foo; return *this; }
   Bar& operator = (signed int foo) { m = foo; return *this; }
 };

You could substitute pretty much any number integral assignment operators.

Now say:

Bar bar; bar = Uno; //error: assignment operators are ambiguous

Whereas, with the Windows compiler (msvc for short) Uno will be treated as
an int (signed -- perhaps more specifically a const long int if the option
exists) ...basicly msvc resolves this ambiguity by choosing the closest
match on a well defined top down prioritized basis, whereas GCC just gives
up and declares ambiguity. 

Its mostly just annoying... turns out though I already had an alternative
assignment for all of the cases I've ran into so far during the porting
process.

So I hope that clears everything up. Appreciate your curiosity.

sincerely,

michael

PS: I will go ahead and repost this in the Nabble forums if it doesn't pop
up (my apologies if it is multiposted to the mailing lists etc)



-- 
View this message in context: http://www.nabble.com/Does-GCC-assign-any-type-to-enums-that-can-be-specified-in-an-assignment-operator--tf3967638.html#a11269077
Sent from the gcc - Help mailing list archive at Nabble.com.



More information about the Gcc-help mailing list