C++11 constexpr doesn't allow to use a static_assert to check the argument of a constexpr function ; C++11 doesn't actually allow a lot of things beside return statement in constexpr function, and while static_assert is allowed in a constexpr function body it can't access the function input.
Which means something like :
constexpr int f(int x)
{
static_assert(x > 0);
return (x > 2) ? x + 1 : x - 3;
}
won't compile because x is an argument of f and not some template arg.
It makes sense from a formal point of view since a constexpr function can be called with non constexpr argument and thus evaluated at runtime.
But from a practical point of view this means that some template construct are required to implement compile time argument checks, and the idea behind constexpr is to alleviate struggle when doing meta programming by providing a more lightweight syntax.
Is there some alternative way to have compile time check inside a constexpr expression ? With Google I found that throwing exception can cause a compile time error but I had no luck with MSVC2015 : it throws when I start my app but it still builds...
Vincent