-1

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.

1

1 Answer 1

2

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.
Sign up to request clarification or add additional context in comments.

1 Comment

see also here about fixing bug in MVSC

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.