I have some templated code for timers abstraction on my avr device. Most relevant part here is:
template <typename T> class Timerx8bit { T reg; static_assert(T::res == TimerResolution::bit8, "The timer template is 8bit, registers must also be 8bit"); } struct Timer0 { constexpr static const TimerResolution res = TimerResolution::bit16; volatile uint8_t* tccra = &TCCR0A; volatile uint8_t* tccrb = &TCCR0B; //[...] }; Now I feed the Timer0 to the template. The problem with that code is, that the static assert seems to evaluate always to true, although in the above situation it should fail. TimerResolution is just an enum class.
The problem seems to be in the template, if I put "TimerResolution::bit8 != TimerResolution::bit8" as the condition, compilation fails as expected, but "T::res != T::res" compiles without any problems... what am I missing here?
EDIT: While preparing full example of code I found the problem, although I still don't quite understand why it behaves that way. First, the code:
enum class TimerResolution_test { bit8, bit16 }; struct Timer0_test { constexpr static const TimerResolution_test res = TimerResolution_test::bit8; }; template <typename T> class Timerx8bit_test { public: constexpr static const TimerResolution_test res = TimerResolution_test::bit8; private: T reg; static_assert(T::res != T::res, "The timer template is 8bit, registers must also be 8bit"); }; template<typename Timer> class pwm_test { }; Instantiation:
pwm_test<Timerx8bit_test<Timer0_test>> testTimer; // Compiles Timerx8bit_test<Timer0_test> testTimer2; // Fails The second instantiation fails with the static_assert as above. If I put 'false' instead of the templated condition it fails in both cases... why is that? Shouldn't it fail in both cases with the original templated condition?
TimerResolution? What isTimerResolution::bit8? If possible please try to create a Minimal, Complete, and Verifiable Example and show us.