cast to an enum does not throw errors even for invalid values

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

 



Using GCC 4.1.2.

(1)

Consider:

enum BODY { SUN, MOON, STAR } ;
enum PLANET { EARTH, VENUS, MARS, PLUTO } ;
int main ( void )
{
 BODY body ;
 // body = 1 ;                           // gives error. expected.
 // body = EARTH ;                       // gives error. expected.
 body = (BODY) 1 ;                       // no error. expected.
 body = (BODY) EARTH ;                   // no error. expected.
 body = static_cast < BODY > ( 1 ) ;     // no error. expected.
 body = static_cast < BODY > ( EARTH ) ; // no error. expected.
 body = (BODY) 3 ;                       // no error. unexpected.
 body = (BODY) PLUTO ;                   // no error. unexpected.
 body = static_cast < BODY > ( 3 ) ;     // no error. unexpected.
 body = static_cast < BODY > ( PLUTO ) ; // no error. unexpected.
}

I feel that the compiler should detect it when a value being casted to an enum does not have an equivalent enum identifier. i.e. in the above case, 3 and PLUTO (equivalent to 3 from the PLANET enum) do not have an equivalent identifier in the BODY enum. But still the compiler does not call an error. Even negative integers are "casted" to the target enum without an error.

If an error is not called, I feel it defeats the very meaning of casting, to convert an object of one type to an object of another type. When there is no equivalent object of the target type, how can the casting happen?

Even the behaviour of static_cast < unsigned int > ( -3 ) which gives 4294967293 is somehow understandable, since the internal binary representation of signed int -3 and unsigned int 4294967293 is the same (correct me if I'm wrong). So there can be said to be some kind of "equivalence" which carries the casting to its goal. But how can (BODY) 3 or static_cast < BODY > ( 3 ) in the above example be carried out?

So it seems to me that there is a bug in G++.

(2)

Here's a test program for runtime case:

enum BODY { SUN, MOON, STAR } ;
enum PLANET { EARTH, VENUS, MARS, PLUTO } ;
void check ( PLANET planet )
{
 BODY body ;
 body = (BODY) planet ;
 body = static_cast < BODY > ( planet ) ;
}
int main ( void )
{
 check ( EARTH ) ;
 check ( PLUTO ) ;
}

The program executes without errors. It should throw a runtime error.

Both these are only in C++. In C, since enum-s are still fully equivalent to int-s, the question of casting does not rise at all.

(3)

Incidentally, (unsigned int) (-3) in C (processed by GCC) gives me still -3, and I don't know whether this is expected behaviour.

Thanks for your feedback. If it is judged by the community that any or all of the behaviours I observe above is really a bug, I will report it.

Shriramana Sharma.


[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