This is closely related to Are C++ enums signed or unsigned?. According to JavaMan's answer, an enum
is neither signed
nor unsigned
. But it does follow integral promotion rules.
I'm working with a library that uses enums
and then passes them to other class objects that expect mostly unsigned
types (like unsigned int
and size_t
). Enabling the -Wsign-conversion
warning in an effort to catch legitimate mistakes is causing a number false positives due to the language's rules.
The rule kind of creates a a situation where its difficult to ensure type safety and catch common mistakes. Its difficult because I want to avoid things like static_cast
sprinkled liberally throughout the code.
Is there a way to override the language's default behavior for promoting enums
to concrete signed
or unsigned
types? (Similar to the way you can specify a char
is signed or unsigned).
Related, the library was written in the 1990s, so it supports a number of older compilers. It would be great if the solution addressed even C++03 and possibly earlier.
From How to guard move constructors for C++03 and C++11?, I know there's no reliable way in practice to detect when other C++ language variants are in effect. It fell flat on its face during testing with Clang 3.5 using -std=c++03
and -std=c++11
.