c++11 lambdas 会捕获它们不使用的变量吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6181464/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 19:38:49  来源:igfitidea点击:

Do c++11 lambdas capture variables they don't use?

c++lambdac++11

提问by HighCommander4

When I use [=]to indicate that I would like all local variables to be captured by value in a lambda, will that result in alllocal variables in the function being copied, or just all local variables that are used by the lambda?

当我[=]用来表示我希望通过 lambda 中的值捕获所有局部变量时,是否会导致复制函数中的所有局部变量,或者仅复制lambda 使用的所有局部变量?

So, for example, if i I have:

因此,例如,如果我有:

vector<int> my_huge_vector(100000);
int my_measly_int;
some_function([=](int i){ return my_measly_int + i; });

Will my_huge_vector be copied, even though I don't use it in the lambda?

即使我不在 lambda 中使用 my_huge_vector,也会复制它吗?

采纳答案by James McNellis

Each variable expressly named in the capture list is captured. The default capture will only capture variables that are both (a) not expressly named in the capture list and (b) usedin the body of the lambda expression. If a variable is not expressly named and you don't use the variable in the lambda expression, then the variable is not captured. In your example, my_huge_vectoris not captured.

捕获列表中明确命名的每个变量都被捕获。默认捕获将仅捕获 (a) 未在捕获列表中明确命名和 (b)在 lambda 表达式主体中使用的变量。如果变量未明确命名并且您不在 lambda 表达式中使用该变量,则不会捕获该变量。在您的示例中,my_huge_vector未捕获。

Per C++11 §5.1.2[expr.prim.lambda]/11:

根据 C++11 §5.1.2[expr.prim.lambda]/11:

If a lambda-expressionhas an associated capture-defaultand its compound-statementodr-usesthisor a variable with automatic storage duration and the odr-usedentity is not explicitly captured, then the odr-usedentity is said to be implicitly captured.

如果λ-表达具有相关联捕捉默认及其化合物语句ODR用途this或具有自动存储持续时间和所述的可变ODR使用的实体没有明确地捕获,则ODR使用的实体被认为是隐式地捕获。

Your lambda expression has an associated capture default: by default, you capture variables by value using the [=].

您的 lambda 表达式具有关联的捕获默认值:默认情况下,您使用[=].

If and only if a variable is used (in the One Definition Rule sense of the term "used") is a variable implicitly captured. Since you don't use my_huge_vectorat all in the body (the "compound statement") of the lambda expression, it is not implicitly captured.

当且仅当使用了一个变量(在术语“使用”的单一定义规则意义上)才是隐式捕获的变量。由于您根本不使用my_huge_vectorlambda 表达式的主体(“复合语句”),因此不会隐式捕获它。

To continue with §5.1.2/14

继续第 5.1.2/14 节

An entity is captured by copy if

  • it is implicitly captured and the capture-defaultis =or if
  • it is explicitly captured with a capture that does not include an &.

一个实体被副本捕获,如果

  • 它是隐式捕获的,捕获默认值=或如果
  • 它是用不包含&.

Since your my_huge_vectoris not implicitly captured and it is not explicitly captured, it is not captured at all, by copy or by reference.

由于您my_huge_vector没有被隐式捕获,也没有被显式捕获,因此根本不会通过复制或引用来捕获它。

回答by Thomas Minor

No, my_huge_vectorwill not be captured. [=]means all usedvariables are captured in the lambda.

不,my_huge_vector不会被捕获。[=]意味着所有使用的变量都在 lambda 中捕获。