samedi 28 janvier 2017

static_assert on reference template argument type

What I'm trying to do is this simple template clamp function. I want to ensure upper >= lower either in 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 non-constexpr arguments, 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 properly? When called with constexpr (say, clamp<int>(0, 10, myvar) the static_assert should be fired, otherwise the usual dynamic assert will do?

Aucun commentaire:

Enregistrer un commentaire