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

Shriramana Sharma samjnaa@gmail.com
Mon May 28 14:17:00 GMT 2007


John Love-Jensen wrote:

> That, obviously, is NOT what you desire.  You want some insurance that the
> right hand value is checked and verified to be cast to the left hand BODY
> value.
> It is, perhaps, unfortunate that the compiler does not synthesize this for
> you.  But that's just not C++.

See the following code:

struct Struct1
{
     int a, b ;
} ;
struct Struct2
{
     int a, b ;
} ;
int main ( void )
{
     Struct1 s1 ; Struct2 s2 ;
     s1 . a = 2 ; s1 . b = 3 ;
     // s2 = static_cast < Struct2 > ( s1 ) ; // gives error: no 
matching function for call to ‘Struct2::Struct2(Struct1&)’
}

So the compiler itself checks whether it is possible to convert one 
struct value to another when a static cast is requested. Similarly for 
classes. I feel GCC can implement the same for enum-s too.

If it is not part of the C++ definition, then please tell me where I can 
include a request for such a provision in the next C++ standard.

Shriramana Sharma.



More information about the Gcc-help mailing list