user767856
user767856

Reputation: 75

Can you give an example regarding this point: lambda-expression

A point from n3290:ISO Standared draft, §5.1.2/9

A lambda-expression whose smallest enclosing scope is a block scope (3.3.3) is a local lambda expression; any other lambda-expression shall not have a capture-list in its lambda-introducer. The reaching scope of a local lambda expression is the set of enclosing scopes up to and including the innermost enclosing function and its parameters. [ Note: This reaching scope includes any intervening lambda-expression's — end note ]

Can any one give an exmaple for the above point, especially: "other lambda-expression shall not have a capture-list in its lambda-introducer." Where does this situation arise?

Upvotes: 3

Views: 357

Answers (1)

Xeo
Xeo

Reputation: 131829

The situation should theoretically arise in namespace scope, as @Space_C0wb0y shows in his comment link.

#include <iostream>

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

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

If find it strange that GCC accepts that snippet, as MSVC correctly rejects it with the following error message:

error C3480: 'x': a lambda capture variable must be from an enclosing function scope

Upvotes: 6

Related Questions