Say we have this:
template<typename T, typename Compare>
constexpr const T& max(const T& first, const T& second, Compare comp)
{
return comp(first, second) ? first : second;
}
template<typename T>
constexpr const T& max(const T& first, const T& second)
{
return max(first, second, [](const auto& i, const auto& j) { return i > j; });
}
Simple definition of a max
function, right? Compiles all fine, since constexpr
is free to choose when to compute at compile time and when not.
But when I do:
static_assert(max(2, 5) == 5, "Boom");
It fails. Clang tells me that the lambda is not valid in a constant expression. And I'm forced to use std::greater<>()
instead.
As far as I understand, lambdas are just temporary function objects. Can anyone tell me why they decided not to make them constexpr
-friendly? Or is my syntax wrong?
Aucun commentaire:
Enregistrer un commentaire