如何解决 g++ 不支持 auto 自动数据类型?
在编写 Lambda 表达式的时候,发现 gcc 似乎不支持 auto 类型。
源代码 test.cpp 如下:
#include <iostream>
using namespace std;
int main() {
auto basicLambda = [] { cout << "Hello, world!" << endl; };
basicLambda();
return 0;
}
编译报错:
$ g++ test.cpp -o test
test.cpp: In function ‘int main()’:
test.cpp:6:7: error: ‘basicLambda’ does not name a type
auto basicLambda = [] { cout << "Hello, world!" << endl; };
^
test.cpp:7:14: error: ‘basicLambda’ was not declared in this scope
basicLambda();
^
经过查询,发现 gcc 是 4.8.4 版本,应该是默认不开启,但是可以通过添加编译参数 -std=c++11 解决问题,
$ g++ test.cpp --std=c++11 -o test $ ./test Hello, world!