Under some situations, it seems like I can access functions that should be in the std namespace without a using
or std::
qualifier. So far, I've only seen this occur with functions from the algorithm
library.
In the following example, I expect all_of()
to be in the std namespace, but this code compiles without error in VS2013 (Microsoft Compiler 18).
#include <iostream>
#include <string>
#include <algorithm>
int main() {
const std::string text = "hey";
std::cout << all_of(begin(text),end(text),islower);
return 0;
}
Changing std::cout
to cout
without adding a using namespace std
or using std::cout
generates an "undeclared identifier" error as expected.
What's going on here?
This probably happens due to Argument-Dependent Lookup. The iterator returned by begin(text)
and end(text)
is probably a class defined in namespace std
(or nested in a class in namespace std
), which makes namespace std
associated with it. Looking up unqualified names for function calls looks into associated namespaces, and finds all_of
there.
By the way, this is exactly the same reason why calling begin(text)
works, even though the function template begin()
is defined in namespace std
. text
is a std::basic_string
, so std
is searched.