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

John Love-Jensen eljay@adobe.com
Tue May 29 00:23:00 GMT 2007


Hi Shriramana,

> 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.

Then use classes and structs.

I usually wrap my enum's inside a class to perform the self-same kind of
validity checking that you are requesting.

> I feel GCC can implement the same for enum-s too.

GCC cannot implement the same for enum's too, because then the resulting
language would be not-C++, which is not in the best interest of GNU and GCC.

However, if these kinds of issues are important to you (and they seem to be;
and I have to admit that they are actually quite important to me too), I
strongly recommend taking a look at other languages that have far more
stringent type safety semantics such as Ada or D Programming Language.

Note: both Ada and D Programming Language are available in the GCC family.
Ada is part of the GCC in the gcc-ada-4.2.0.tar.bz2 front end, and D
Programming Language is available as a third party front end for GCC, via
SourceForge <http://dgcc.sourceforge.net/>.

Even Java J2SE 5.0 has impressively nice enum's.  But there's not a Java
J2SE 5.0 for the GCC family yet.  I may be mistaken. but I think there's
active, on-going work being done towards Java J2SE 5.0 support in the GCC
family.  Please bear in mind that Java support in GCC is tricky and in
arrears because Java is a moving target and GCC support of Java is often
after the "fact" (Sun's canonical Java release).

> 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.

It's not part of the C++ definition.

I highly doubt it would even become part of the C++ definition, since it
would be a major breach of backwards compatibility.  Consider enum's used at
bitflags, which can be OR'd together.  Consider code that blithely
interchanges enum's with int's which otherwise is perfectly valid code.

But if you want to pursue introducing a feature request to C++, please
contact ISO/IEC JTC1/SC22/WG21 The C++ Standards Committee:

http://www.open-std.org/jtc1/sc22/wg21/

HTH,
--Eljay



More information about the Gcc-help mailing list