The following code does not compile in clang (it does in GCC):
struct A{
int a;
};
auto test(){
constexpr A x{10};
return []{
return x; // <-- here x is A: clang doesn't compile
}();
}
Clang's error is variable 'x' cannot be implicitly captured in a lambda with no capture-default specified, but I thought that constexpr variables were always captured.
If x is an int, the code compiles:
auto test(){
constexpr int x{10};
return []{
return x; // <-- here x is int: clang is ok
}();
}
Interestingly, the following code also compiles:
auto test(){
constexpr A x{10};
return []{
return x.a;
}();
}
Is clang right? If so, what is the rationale? I'm using -std=c++17
--EDIT--
The following question: Can I use a constexpr value in a lambda without capturing it? is not related to this one, as with clang11 it is not anymore an issue: in fact, as stated above, if x is an int, clang11 compiles.
Sample code also present in https://godbolt.org/z/rxcYjz
int
; it's that you've named a member (which is an odr-use ofx
). It's the same question and bug.x
itself is not anint
in either of your examples. – Asteroids With Wings