Can you give an example regarding this point: lambda expression

Point n3290: ISO Standared draft, ยง5.1.2 / 9

A lambda expression, the smallest covering area of โ€‹โ€‹which is a block region (3.3.3) - a local lambda expression; any other lambda expression should not have a capture list in its lambda introducer. The achieved volume of the local lambda expression is a set of covering areas to and including the inner closing function and its parameters. [Note: this scope includes any intermediate lambda expression - end note)

Can someone give an example for the above paragraph, especially: "another lambda expression should not have a capture list in its lambda introdator." Where does this situation arise?

+3
source share
1 answer

The situation should theoretically arise in the field of namespace, as @ Space_C0wb0y shows in the link to the comment .

#include <iostream>

int x = 12;
auto l = [&x](){ return x; };

int main() {
    std::cout << l() << std::endl;
}

If it seems strange to you that GCC accepts this snippet, as the MSVC correctly rejects it with the following error message:

error C3480: 'x': lambda capture variable must be from the scope of the closing function

+6
source

All Articles