public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* cast to an enum does not throw errors even for invalid values
@ 2007-05-25 11:26 Shriramana Sharma
  2007-05-25 14:38 ` Ian Lance Taylor
  2007-05-25 16:54 ` Young, Michael
  0 siblings, 2 replies; 9+ messages in thread
From: Shriramana Sharma @ 2007-05-25 11:26 UTC (permalink / raw)
  To: gcc-help

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.



^ permalink raw reply	[flat|nested] 9+ messages in thread
* cast to an enum does not throw errors even for invalid values
@ 2007-05-22 13:00 Shriramana Sharma
  2007-05-22 17:09 ` John Love-Jensen
  0 siblings, 1 reply; 9+ messages in thread
From: Shriramana Sharma @ 2007-05-22 13:00 UTC (permalink / raw)
  To: gcc-help

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.

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2007-05-29  0:23 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-05-25 11:26 cast to an enum does not throw errors even for invalid values Shriramana Sharma
2007-05-25 14:38 ` Ian Lance Taylor
2007-05-25 16:54 ` Young, Michael
  -- strict thread matches above, loose matches on Subject: below --
2007-05-22 13:00 Shriramana Sharma
2007-05-22 17:09 ` John Love-Jensen
2007-05-28 14:17   ` Shriramana Sharma
2007-05-28 17:33     ` Sergei Organov
2007-05-29  0:23     ` John Love-Jensen
2007-05-29 11:05       ` Gabriel Dos Reis

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).