bendervader
bendervader

Reputation: 2660

Conditional static_assert

Is there an elegant way to perform a conditional static_assert in c++11

For example:

template <class T>
class MyClass
{
    COMPILE_TIME_IF( IsTypeBuiltin<T>::value)
       static_assert(std::is_floating_point<T>::value, "must be floating pt");
};

Upvotes: 5

Views: 2676

Answers (1)

Biffen
Biffen

Reputation: 6354

Simple boolean logic within static_assert() should do it:

static_assert(
  (!std::is_fundamental<T>::value)
  || std::is_floating_point<T>::value,
  "must be floating pt"
);

I.e. T is either not fundamental or it's floating point. In other words: If T is fundamental it must also be floating point.

Upvotes: 10

Related Questions