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

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

 



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.


[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