Code first, we have the following piece of code that is used to accumulate a constexpr std::array
in compile time:
template <typename T, std::size_t N, typename O>
constexpr T compile_time_accumulator(const std::array<T, N> const &A, const std::size_t i, const O& op, const T initialValue)
{
return (i < N)
? op(A[i], compile_time_accumulator(A, i + 1, op, initialValue))
: initialValue;
}
and the following code example to test/varify it (i.e., that it evaluates in compile time):
constexpr std::array<int, 4> v {{4, 5, 6, 7}};
std::cout << std::integral_constant<int, compile_time_accumulator(v, 42, std::plus<int>())>::value
<< std::endl;
Now if change the operator std::plus<int>
with a constexpr
lambda:
constexpr auto lambda_plus = [] (int x, int y) { return x + y; };
and call it like below:
constexpr std::array<int, 4> v {{4, 5, 6, 7}};
std::cout << std::integral_constant<int, compile_time_accumulator(v, 42, lambda_plus)>::value << std::endl;
^^^^^^^^^^^
I get an error, that lambda is not constexpr
:
call to non-constexpr function ''
Now doing a litle research I discovered that constexpr
lambdas aren't support yet.
Q:
Why if constexpr
lambdas aren't supported, we are allowed to define a constexpr
lambda in the first place?
Edit:
It seems that clang doesn't accep the code. So which compiler is right?