【发布时间】:2017-06-19 01:57:51
【问题描述】:
当捕获 hello 作为参考时,我希望能够在我的 lambda 函数之外修改 hello,但以下代码以相同的 hello 结尾。
#include <iostream>
#include <algorithm>
using namespace std;
int main(){
char hello[] = {"Hello, World!"};
auto up = [&, hello] (char c) {
if (!isupper(c))
{
c = toupper(c);
}
};
for_each(hello, hello + sizeof(hello), up);
cout<<hello<<endl;
}
Hello, World!
当使用 c 作为参考参数按值传递 hello 时,我得到了预期的结果。
#include <iostream>
#include <algorithm>
using namespace std;
int main(){
char hello[] = {"Hello, World!"};
auto up = [hello] (char& c) {
if (!isupper(c))
{
c = toupper(c);
}
};
for_each(hello, hello + sizeof(hello), up);
cout<<hello<<endl;
}
cout<<hello<<endl;
HELLO, WORLD!
我的理解是当 hello 被 value 捕获时,up 会得到一个本地的 hello 副本。暗示 c 是否为引用 hello 不会被修改。但在我的示例中,c 充当对 hello 的非本地副本的引用。我觉得我好像遗漏了一些基本的参考。
【问题讨论】:
-
您的 lambda 表达式都不使用捕获的实体。
-
你对 lambda 捕获的理解是完全错误的。您展示的两个 lambda 表达式都没有使用
hello,因此无论您是通过值还是通过引用来捕获它(在这两种情况下,您都是通过值来捕获它)绝对没有区别,无论如何。您的 lambdas 唯一需要捕获的是您的 lambdas 引用的对象。您的 lambda 表达式中没有任何内容引用hello,因此您捕获hello的方式会有所不同。 -
顺便说一句,没有理由检查
!isupper(c),因为toupper会为您检查,如果它不是小写字母,则简单地返回未修改的字符。