[英]Trouble compiling a simple C++0x program with lambdas
我正在尝试运行一个简单的 lambda 示例。
// lambda.cpp
#include <functional>
//#include <tr1/functional>
int main()
{
// Assign the same lambda expression to a function object.
function<int (int, int)> f2 = [] (int x, int y) { return x + y; };
//function<int (int, int)> f2 = [] (int x, int y) { return x + y; };
}
我正在像这样编译它:
$ g++ -std=c++0x -fpermissive lamdas.cpp
lambdas.cpp: In function ‘int main()’:
lambdas.cpp:10: error: expected primary-expression before ‘=’ token
lambdas.cpp:10: error: expected primary-expression before ‘[’ token
lambdas.cpp:10: error: expected primary-expression before ‘]’ token
lambdas.cpp:10: error: expected primary-expression before ‘int’
lambdas.cpp:10: error: expected primary-expression before ‘int’
lambdas.cpp:10: error: expected ‘;’ before ‘{’ token
如何让它编译没有错误?
您是说std::function
吗?
标准库功能位于std
命名空间中。
有趣的是,您的复制/粘贴显然是假的; 你写了“lamdas.cpp”然后编译了“lambdas.cpp”!
std::function<int (int, int)> f2 = [] (int x, int y) { return x + y; };
或者,可能更好
auto f2 = [] (int x, int y) { return x + y; };
在我看来,您忘记了 -std=c++0x。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.