[英]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.