什么都不捕获的 lambda 可以访问全局变量吗?
Can a lambda capturing nothing access global variables?
int n;
int main()
{
[](){ n = 0; }(); // clang says "ok"
int m;
[](){ m = 0; }(); // clang says "not ok"
}
我只是想知道:
如果 lambda 不捕获任何内容,是否允许按照 C++ 标准访问全局变量?
是的,当然。适用正常名称查找规则。
[expr.prim.lambda]/7 ... for purposes of name lookup ... the compound-statement is considered in the context of the lambda-expression.
回复:为什么局部变量与全局变量的处理方式不同。
[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.
在您的示例中,m
是一个从 lambda 的到达范围自动存储持续时间的变量,因此应被捕获。 n
不是,所以不一定是。
实际上 [](){ n = 10; }();
不捕获任何东西,它使用全局变量代替。
int n;
int main()
{
[](){ n = 10; }(); // clang says "ok"
std::cout << n; // output 10
}
capture-list - a comma-separated list of zero or more captures, optionally beginning with a capture-default.
Capture list can be passed as follows (see below for the detailed
description):
- [a,&b] where a is captured by copy and b is captured by reference.
- [this] captures the current object (*this) by reference
- [&] captures all automatic variables used in the body of the lambda by reference and current object by reference if exists
- [=] captures all automatic variables used in the body of the lambda by copy and current object by reference if exists
- [ ] captures nothing
默认访问全局、静态和常量变量:
#include <iostream>
int n;
int main()
{
[](){ n = 10; }();
std::cout << n << std::endl;
static int m = 1;
[](){ m = 100; }();
std::cout << m << std::endl;
const int l = 200;
[](){ std::cout << l << std::endl; }();
}
int n;
int main()
{
[](){ n = 0; }(); // clang says "ok"
int m;
[](){ m = 0; }(); // clang says "not ok"
}
我只是想知道:
如果 lambda 不捕获任何内容,是否允许按照 C++ 标准访问全局变量?
是的,当然。适用正常名称查找规则。
[expr.prim.lambda]/7 ... for purposes of name lookup ... the compound-statement is considered in the context of the lambda-expression.
回复:为什么局部变量与全局变量的处理方式不同。
[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.
在您的示例中,m
是一个从 lambda 的到达范围自动存储持续时间的变量,因此应被捕获。 n
不是,所以不一定是。
实际上 [](){ n = 10; }();
不捕获任何东西,它使用全局变量代替。
int n;
int main()
{
[](){ n = 10; }(); // clang says "ok"
std::cout << n; // output 10
}
capture-list - a comma-separated list of zero or more captures, optionally beginning with a capture-default.
Capture list can be passed as follows (see below for the detailed description):
- [a,&b] where a is captured by copy and b is captured by reference.
- [this] captures the current object (*this) by reference
- [&] captures all automatic variables used in the body of the lambda by reference and current object by reference if exists
- [=] captures all automatic variables used in the body of the lambda by copy and current object by reference if exists
- [ ] captures nothing
默认访问全局、静态和常量变量:
#include <iostream>
int n;
int main()
{
[](){ n = 10; }();
std::cout << n << std::endl;
static int m = 1;
[](){ m = 100; }();
std::cout << m << std::endl;
const int l = 200;
[](){ std::cout << l << std::endl; }();
}