1 msgProblem installing gcc-2.96+ or gcc 4.2 beta in...
1 msgKeo Souvannasy est absent(e).
2 msg-g vs -g3 -ggdb3
2 msgProblems properly escaping backslashes in dos p...
6 msgNew -ffriend-injection behavior - is it really ...
1 msgautovectorization capabilities
10 msgwhere to discuss problems with IEEE 754 standar...
1 msgDebugging OpenMP with gcc 4.2 and gdb 6.6
5 msg4.2.0 installation problem - `bison' missing on...

cast to an enum does not throw errors even for ...
\ Shriramana Sharma (22 May 2007)
. \ John Love-Jensen (22 May 2007)
. . \ Shriramana Sharma (28 May 2007)
. . . \ John Love-Jensen (28 May 2007)
. . . . \ Gabriel Dos Reis (29 May 2007)
. . \ Sergei Organov (28 May 2007)
. \ Shriramana Sharma (25 May 2007)
. . \ Ian Lance Taylor (25 May 2007)
. . \ Young, Michael (25 May 2007)

2 msgEnabling basic block profiling information
1 msghelp writing gcc code
1 msg***JOB OFFER
1 msgQuestion on object code layout
5 msgRe: Problem when using optimization on aix 5.2 ...
2 msgPRECISE gcc Intel conventions
1 msgFriendly gcc hacker needed
2 msgBuilding GCC for 64-bit architecture
1 msgListing of inlined functions
5 msggcc behind the scenes
Subject:cast to an enum does not throw errors even for invalid values
Group:Gcc-help
From:Shriramana Sharma
Date:22 May 2007


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.



© 2004-2008 readlist.com