Can a lambda capturing nothing access global variables?

Yes, sure. Normal name lookup rules apply.

[expr.prim.lambda]/7 … for purposes of name lookup … the compound-statement is considered in the context of the lambda-expression.

Re: why local variables are treated differently from global ones.

[expr.prim.lambda]/13 … If a lambda-expression or an instantiation of the function call operator template of a generic lambda odr-uses (3.2) this or a variable with automatic storage duration from its reaching scope, that entity shall be captured by the lambda-expression.

[expr.prim.lambda]/9 A lambda-expression whose smallest enclosing scope is a block scope (3.3.3) is a local lambda expression… 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.

In your example, m is a variable with automatic storage duration from the lambda’s reaching scope, and so shall be captured. n is not, and so doesn’t have to be.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)