C++11 / g++ : std:: lambda 中需要限定符,尽管“使用命名空间 std”是必需的。被给予
我试图发现新的 C++11 标准(使用 g++ 4.6.2)的一些优点。在“all_of”算法函数中使用 lambda 时,我遇到了 std:: 限定符的奇怪问题。
我正在“使用”std 命名空间,如代码片段开头所示。这使得 for 循环中的pair变量的声明定义良好。
但是,我在“all_of”算法中使用的 lambda 参数中尝试了相同的操作。在我意识到完整的 std:: 合格的 std::pair 可以在那里工作之前,我遇到了一些难以理解的错误消息,但只有pair 不行。
我错过了重要的一点吗? lambda 的声明发生在这个文件中,所以命名空间在这里应该仍然是活动的,对吧?或者所需的 std:: 限定符是否依赖于不同文件中的某些 STL 代码?或者这可能是 g++ 中的一个错误?
此致, Peter
PS:代码编译时没有粘贴此处的警告,但是删除 all_of lambda 中的 std:: 时,我收到一条错误消息。
#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!";
};
}
编辑:刚刚注意到我收到了对这个老问题的否决票。没问题,但请详细说明原因。它将帮助我改进未来的问题,最终整个社区都会受益。谢谢!
I was trying to discover some of the goodies of the new C++11 standard (using g++ 4.6.2). Playing around with lambdas in a an "all_of" algorithm function, I encountered a strange problem with the std:: qualifier.
I am "using" the std namespace as shown at the beginning of the code snippet. This makes the declaration of the pair variable in the for loop well-defined.
However, I tried the same in the lambda argument used in the "all_of" algorithm. I came across several hard-to-understand error messages, before I realized that a full std:: qualified std::pair would work there, but only pair not.
Am I missing an important point? The declaration of the lambda happens in this file, so the namespace should still be active here, right? Or does the required std:: qualifier depend on some STL code in a different file? Or is it likely to be a bug in g++?
Best regards,
Peter
PS: the code compiles without warnings as pasted here, but removing the std:: in the all_of lambda, I get an error message.
#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!";
};
}
Edit: Just noticed I received a downvote for this old question. No problem with that, but please elaborate on the reasons. It will help me improve future questions, and in the end the entire community will profit. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
重命名 for 循环中的变量
pair
。它的范围应该仅扩展到
for
循环的末尾,因此不会干扰您的lambda,但 g++ 有一些用于古老的 for 范围规则的代码,但情况并非如此,因此它可以为古老的 C++ 代码发出更好的错误消息。
看起来该兼容性代码中存在错误。
Rename the variable
pair
in your for loop.It's scope should only extend to the end of the
for
loop and therefore not interfere with yourlambda, but g++ has some code for ancient for-scoping rules where that was not the case, so it can emit better error messages for ancient C++ code.
It looks as if there is a bug in that compatibility code.