1
我有涉及,編譯罰款拉姆達一小段程式碼,但智能感知拋出一個錯誤。VS Express 2012 C++ lambda Intellisense錯誤?
items.erase(remove_if(begin(items), end(items),
[](const Item& mItem){ return mItem.booleanMember; }),
end(items));
的錯誤是
IntelliSense: more than one conversion function from "lambda []bool (const Item &mItem)->bool" to "<error-type>" applies:
function "lambda []bool (const Item &mItem)->bool::operator bool (*)(const Item &mItem)() const"
function "lambda []bool (const Item &mItem)->bool::operator bool (*)(const Item &mItem)() const"
function "lambda []bool (const Item &mItem)->bool::operator bool (*)(const Item &mItem)() const"
如果我更改代碼以
items.erase(remove_if(begin(items), end(items),
[&](const Item& mItem)->bool{ return mItem.booleanMember; }),
end(items));
然後錯誤消失。我會很好,但我不完全知道發生了什麼。我知道我沒有明確指定一個布爾返回值(以及與此改變的代碼,我),但我覺得我沒必要,如果有在lambda只有一行。另外,我不知道是什麼[&]在做什麼。
不,你不需要在這種情況下,顯式的返回類型。並且[&]是捕獲條款,在許多lambda介紹中進行了解釋。 – chris