I am trying to make adjusted enumerations in my program compatible with the base type, but the following code does not work. Is this because of poor support for C ++ 11 in the compiler I use (VC11), or is it because the code violates some rules from the C ++ 11 standard? In the latter case, what rules are definitely violated (links to specific standard offers are welcome)?
#include <type_traits> enum class Test: short int { A,B,C }; template<typename E> bool operator != (E e, typename std::underlying_type<E>::type n) { return static_cast<typename std::underlying_type<E>::type>(e) != n; } template<typename E> bool operator != (typename std::underlying_type<E>::type n, E e) { return static_cast<typename std::underlying_type<E>::type>(e) != n; } int main() { short int x = 123; x != Test::B; // compilation error }
This is why I think that my code should be compatible with C ++ 11. Quote from the C ++ 11 standard (14.8.3.1):
For each function template, if the argument output and verification succeed, the template arguments (output and / or explicit) are used to synthesize the declaration of one function template specialization, which is added to the candidate functions that will be used when resolving overloads. If,> for a given function template, the argument is not output, then no function is added to the set of> candidate functions for this template.
EDIT. My code is not compatible with C ++ 11 (thanks for explaining Vaughn Caton and Andy Proul). Alternative working code is provided in Andy Prole's answer.
PS In the end, I ended up creating unoccupied enumerations using namespaces:
namespace Test_ { enum Test { A,B,C }; }; using Test_::Test; namespace Test2_ { enum Test2 { Z,Y,B }; }; using Test2_::Test2;
source share