【发布时间】:2018-12-06 03:53:18
【问题描述】:
我的代码如下
#include <iostream>
#include <string>
using namespace std;
int x;
struct A
{
auto func()
{
auto test([&, &x](){cout << x << endl;});
test();
}
};
int main()
{
A a;
x = 5;
a.func();
}
我的程序如上,我是用下面的命令编译的
g++ -std=c++11 ex.cpp -o ex
但是,我收到如下警告
ex.cpp:在成员函数‘auto A::func()’中:
ex.cpp:11:19: 警告:以非自动存储持续时间捕获变量“x”auto test([&, &x](){cout << x << endl;});^
ex.cpp:6:5:注意:此处声明了“int x”int x;
谁能帮我解决?
【问题讨论】:
-
x是全局变量,不需要捕获。 -
而且“全部捕获”也是不需要的。你实际上什么也没捕捉到。
标签: c++ lambda storage duration