Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditional static_assert

Tags:

c++

c++11

c++14

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");
};
like image 798
bendervader Avatar asked Jan 08 '23 14:01

bendervader


1 Answers

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.

like image 199
Biffen Avatar answered Jan 10 '23 03:01

Biffen