Search code examples
c++static-assert

static assert c++ - constant expression error


I'm trying to use the static_assert :

static_assert(std::numeric_limits<T>::max() <= std::numeric_limits<size_t>::max() , "T maximum should be smaller than size_t maximum" );

but I get an error on compilation

Expected constant expression

I do have an expression so whats the problem ?


Solution

  • For a compiler without constexpr use the appropriate C library MAX names such as SIZE_MAX from <stdint.h>.

    Alternatively, for an unsigned type such as size_t you can just use size_t(-1).

    See (http://en.cppreference.com/w/cpp/types/climits) for a list.