我試圖發現一些新的C++ 11標準(使用g ++ 4.6.2) 。在一個「all_of」算法函數中玩弄lambdas,我遇到了std :: qualifier的一個奇怪問題。C++ 11/g ++:lambda需要std :: qualifier,儘管「using namespace std」被給出
我正在使用std命名空間,如代碼片段的開頭所示。這使得for循環中的pair變量聲明定義良好。
但是,我在「all_of」算法中使用的lambda參數中嘗試了相同的操作。在我意識到一個完整的std :: qualified std :: pair可以在那裏工作之前,我遇到了幾個難以理解的錯誤消息,但只有一對沒有。
我錯過了一個重要的觀點嗎? lambda的聲明發生在這個文件中,所以命名空間在這裏仍然是活動的,對吧?或者所需的std :: qualifier是否依賴於不同文件中的某些STL代碼?或者它可能是g ++中的錯誤?
最好的問候, 彼得
PS:代碼編譯沒有警告,如粘貼在這裏,但在取出的std ::在all_of拉姆達,我得到一個錯誤信息。
#include <iostream>
#include <memory>
#include <map>
#include <string>
#include <algorithm>
#include <utility>
using namespace std;
void duckburg() {
const int threshold = 100;
map <string, int> money;
money["donald"] = 200;
money["daisy"] = 400;
money["scrooge"] = 2000000;
// obviously, an "auto" type would work here nicely,
// but this way my problem is illustrated more clearly:
for (const pair <string, int> &pair : money) {
cout << pair.first << "\t" << pair.second << endl;
}
if (all_of(money.begin(), money.end(),
[&](std::pair<string, int> p) {
return bool(p.second > threshold);
}))
{
cout << "yes, everyone is rich!";
} else {
cout << "no, some are poor!";
};
}
編輯:只是注意到,我收到了這個老問題downvote。沒有問題,但請詳細說明原因。這將有助於我改善未來的問題,最終整個社區都將獲益。謝謝!
聽起來像GCC中的錯誤,使用MSVC編譯得很好。不能用Clang進行測試,因爲它們還沒有實現lambda表達式。 FWIW,GCC 4.5.1與[這個最小化的代碼]沒有問題(http://ideone.com/LdGJT)。 – Xeo 2011-12-16 13:19:24
是的,我會在/不用Clang自己提到的std ::中進行測試,但是現在由於缺少lambda表達式而不可能。 – Piotr99 2011-12-16 13:22:31