Why can't a constexpr "variable" be seen only by a capturing lambda ? For me the a constexpr "variable" should have the same meaning as a static const "variable"; and the latter one can be seen inside a lambda without any capturing.
Asked
Active
Viewed 170 times
-2
-
2Maybe https://stackoverflow.com/questions/33873788/can-i-use-a-constexpr-value-in-a-lambda-without-capturing-it would answer your question ? – Laurent Jospin Mar 16 '22 at 07:54
1 Answers
1
As the already linked answer is pretty old, I take the example code from here and it compiles now fine with clang, gcc. MSVC still rejects the code, however. Seems to be a compiler bug.
#include<array>
int main()
{
constexpr int i = 0;
auto f = []{
std::array<int, i> a;
};
return 0;
}
That gcc and clang is right can be read lambda capture here:
captures: A lambda expression can read the value of a variable without capturing it if the variable :
- is constexpr and has no mutable members.

Klaus
- 24,205
- 7
- 58
- 113