Accessing constant variables from a lambda in Visual Studio 2026

1 day ago 1
ARTICLE AD BOX

What changed in C++20 and later versions that caused the program to be rejected?

Flag /permissive- is enabled by default from C++20 in msvc. Microsoft has many extensions that results in successful compilation of an invalid C++ program. If you use /permissive- flag with C++17, you'll notice that msvc also starts rejecting the said program(which is incorrect behavior as the program is well-formed). This means that msvc was already rejecting the program(as far as standard compliance is concerned)


There is a closely related msvc bug that was fixed just 4 days ago(on 19-feb-2023). The reported bug says:

A fix for this issue is now available in preview release. Try out the fix by installing the most recent preview from  https://visualstudio.microsoft.com/vs/preview/.

Richard's user avatar

In C++20, even if a variable is constexpr, if you take its address or pass an array (which decays to a pointer), it is "ODR-used." This requires the variable to be captured.

void f(const void*) {} int main() { constexpr float one[1] = { 1.0f }; // Capture the pointer to the array explicitly [ptr = one]() { f(ptr); f(&ptr[0]); }(); }

0___________'s user avatar

2 Comments

But OP already does capture the variable by refence. And other compilers compile his code just fine godbolt.org/z/5G98nWP9f

2026-02-22T19:35:53.093Z+00:00

I think we was very specific about the compiler. What your exmaple fhave in common with msvc

2026-02-22T20:15:17.837Z+00:00

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.

Read Entire Article