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

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi Michael,

> ... but in this case, there is also an operator for unsigned int
> assignment, so if it shoud resolve to that operator even in this case.

No, it can't resolve because it is ambiguous.

The TYPE of Uno, Dos and Tres is all the same.  On my platform, they're all
signed long long (a 64-bit signed int).

If you want them to each have their own distinct type (which then would not
be ambiguous), you'd have to do:

enum { Uno = -1 }; // signed int
enum { Dos }; // signed int
enum { Tres = 0x80000000 }; // unsigned int

But better yet (far, far better!), don't use an anonymous enum, use a
typename'd enum, and then use that typename specifically in your assignment
operator.

> I think before I had a 'long' in the int types I was using, so that might've
> made a difference.

A long is not legal value for an enum in C89.  (I'm not sure about C99.)
But it is a legal value for C++.

> GCC seems to resolve a 0 as a long int rather than just int.

By itself?  No, a 0 is type biased as an int.  A 0L is type biased as a long
int.

In an enum?  In C++, the type of an enum depends on the values enumerated
taken as a set (with a few caveats and sharp edges for the unwary).

> I could try myself naturally, but I don't suppose its possible to cast enum
> values to say a char or short type?

Something like...

enum signed char { Uno, Dos, Tres };

...or...

signed char enum { Uno, Dos, Tres };

...?

That'd be nice.  A previous compiler I used on the Amiga had an extension
like that, which at that time I used heavily.  Alas, it is not part of the
standard.

> Would a pointer type or something larger than 32bits be accomodated too?

I don't think you can have an enum of pointers.

For C89, if your int size is larger than 32-bit, then that int size in enums
is accommodated.

For C++, it allows enums on long int, and GCC has an extension support for
long long.  Depending on your platform, if long int or long long is larger
than 32-bit, then something larger than 32-bits is accommodated.

Keep in mind that "larger than 32-bits" is not the C/C++ way of thinking
about things.  An int is the natural word size of the architecture (minimum
of 16-bit).  So an int could be 16-bit, or 32-bit, or 64-bit (we're now
seeing 64-bit natural word size architectures in personal computers on
people's desktops), or 128-bit, or 256-bit.

And that's just considering the "8-bit per byte" octet architectures, and
discounting the other bits-per-byte sized architectures.

If you need specific bit-sizes, look at C99's <stdint.h> and the more
extensive companion <inttypess.h>.  If you are doing C++, I recommend
mimicking <stdint.h> ... but PLEASE put the typedef's in your own namespace,
not in the global namespace and not in std namespace.  Or better yet, use
BOOST's "boost/cstdint.hpp" which has done the work for you already.

Sincerely,
--Eljay





[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux