Search code examples
c++11constantsassertconstexprstatic-assert

C++11: std::max(a,b) in static_assert()?


I noticed, that in [24.4.7] of the last C++-Std Doc N3291 max ist not constexpr:

template<class T> const T& max(const T& a, const T& b);

Therefore, it is not allowed to use it in a static_assert for example. Correct?

static_assert( max(sizeof(int),sizeof(float)) > 4, "bummer" );

Solution

  • That is correct.

    I imagine the reason is simply that std::max calls T::operator< for an arbitrary type T and for std::max to be constexpr, it would require T::operator< to be constexpr, which is unknown.