'Why can't a constexpr "variable" be seen only by a capturing lambda?

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.



Solution 1:[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;
}

see on godbolt

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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Klaus