What I'm trying to do is a simple template clamp function. I want to provide upper >= lowerat runtime and at compile time.
template <typename T>
T clamp(const T& lower, const T& upper, const T& n)
{
weak_assert(upper >= lower);
return std::max(lower, std::min(n, upper));
}
It seems reasonable to write:
static_assert(upper >= lower, "invalid bounds");
However, when called with no arguments constexpr, the compiler gives me this:
Static_assert expression is not an integral constant expression
In instantiation of function template specialization 'clamp<int>' requested here
Is there any way to achieve this correctly? When called with constexpr(say clamp<int>(0, 10, myvar)static_assert must be running, otherwise would a normal dynamic assert do it?
Hedin source
share