C++11 / g++ : std:: lambda 中需要限定符,尽管“使用命名空间 std”是必需的。被给予

发布于 2024-12-21 13:51:28 字数 1366 浏览 0 评论 0原文

我试图发现新的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

扎心 2024-12-28 13:51:28

重命名 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 your
lambda, 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文