Given the following code
#include <type_traits>
int main ()
{
std::integral_constant<int, 42> ic;
[&]{ constexpr int i = ic; (void)i; }();
}
(the (void)i;
is just to avoid a "unused variable" warning) clang++ compile without problem where g++ gives the following error
prog.cc: In lambda function:
prog.cc:7:27: error: '__closure' is not a constant expression
7 | [&]{ constexpr int i = ic; (void)i; }();
| ^~
Capturing by value
[=]{ constexpr int i = ic; (void)i; }();
both compilers compile without problem.
The question, as usual, is: who's right? g++ or clang++?
-- EDIT --
As pointed by JVApen, ic
isn't defined constexpr
; so how can a not-constexpr variable initialize a constexpr
variable?
Anyway, the behavior doesn't change defining ic
as constexpr
constexpr std::integral_constant<int, 42> ic;
clang++ compile without problem; g++ gives an error capturing by reference.
-- EDIT --
As pointed by Jans, defining ic
as constexpr
and using aggregate initialization
constexpr std::integral_constant<int, 42> ic{};
both compilers compile without problem.
But without constexpr
std::integral_constant<int, 42> ic{};
g++ gives the usual error.