operator & override for c++ template enum class

  • Thread starter Thread starter J. J. Lee
  • Start date Start date
J

J. J. Lee

Guest
#include <iostream>
#include <type_traits>

template <class V> class Test
{
public:
enum class State : unsigned int
{
NONE = 0x0, START = 0x1, STOP = 0x2
};
};

template <class V> using TestState = typename Test<V>::State;

template <class V> TestState<V> operator &(TestState<V> lhs, TestState<V> rhs)
{
using U = std::underlying_type_t<TestState<V>>;
return static_cast<TestState<V>>(static_cast<U>(lhs) & static_cast<U>(rhs));
}

void test()
{
if ((Test<int>::State::START & Test<int>::State::STOP) == Test<int>::State::NONE) {
std::cout << "a test\n";
}
}

I got a compile error -
Test1.cpp(25,34): error C2676: binary '&': 'Test<int>::State' does not define this operator or a conversion to a type acceptable to the predefined operator

I don't know how to fix this problem, please help.

Thanks a lot.

Continue reading...
 
Back
Top