C ++ 11 / g ++: std :: qualifier required in lambda, although "using the std namespace" is given

I tried to find some of the advantages of the new C ++ 11 standard (using g ++ 4.6.2). When playing with lambdas in the "all_of" algorithm function, I ran into a strange problem with std :: qualifier.

I use the "std namespace" as shown at the beginning of the code snippet. This makes the declaration of the variable pair in the for loop correct.

However, I tried the same thing in the lambda argument, which is used in the all_of algorithm. I came across some hard-to-understand error messages before I realized that the full std :: quald std :: pair would work here, but not a couple.

Did I miss an important point? The declaration of this lambda occurs in this file, so the namespace should still be active here, right? Or does the required std :: qualifier depend on some STL code in another file? Or could it be a bug in g ++?

Regards, Peter

PS: the code compiles without the warnings inserted here, but removing std :: in all_of lambda, I get an error.

#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: I just noticed that I got a lower limit for this old question. There is no problem with this, but please clarify the reasons. This will help me improve future issues, and ultimately the whole community will make a profit. Thanks!

+7
source share
1 answer

Rename the pair variable to the for loop.

The scope should only last until the end of the for loop and therefore not interfere with your lambda, but g ++ has some code for ancient rules to determine the area where it is not, so it can generate better error messages for ancient C ++ code .

There seems to be an error in this compatibility code.

+8
source

All Articles