1

I found myself with this type of code,

template<class T>
void f(T const& t){
        if constexpr(std::is_same<T, double>{}) call_a(t); // call_a only compiles for double
   else if constexpr(std::is_same<T, float >{}) call_b(t); // call_b only compiles for float
   else                                         assert(0 && "not implemented");
}

What would be an idiomatic way to produce a compiler error instead of a runtime error? (a hard error would be ok.)

I though this would work,

   else                                         static_assert(0, "not implemented");

but it makes the code unconditionally not compile.

Of course, I could add outside the condition static_assert(std::is_same<T, double>{} or std::is_Same<T, float> but basically I am repeating the conditions from the if-statement.

alfC
  • 14,261
  • 4
  • 67
  • 118

0 Answers0