Since the C++11 transition GCC outputs a warning "enumeral and non-enumeral type in conditional expression". I'd like to understand the reasoning behind this warning. What are the perils of comparing enum constants?
It is obvious we can get rid of this warning by
-Wno-enum-compare- by explicitly casting to an integral type
But why the hassle? Personally, I always strive at writing warning free code, and usually the warnings emitted by default are quite sensible. For example, it figures that it's dangerous to compare signed and unsigned integers.
But using enums is widely used idiomatic C++ metaprogramming. I am not aware of any alternative, which is similarly readable, concise and to the point and does not require any actual storage.
To quote a concrete example: What can go wrong with the following metafunction, so that a warning would be adequate?
template<class TYPES>
struct MaxSize;
template<>
struct MaxSize<NullType>
{
enum{ value = 0 };
};
template<class TY, class TYPES>
struct MaxSize<Node<TY,TYPES> >
{
enum{ thisval = sizeof(TY)
, nextval = MaxSize<TYPES>::value
, value = nextval > thisval? nextval:thisval
};
};
Aucun commentaire:
Enregistrer un commentaire