Why does using for_each
as a global function work with GCC?
I would have thought if I did not include the std namespace in my namespace, e.g. using namespace std;
then all std functions and classes would have to be prefixed with std::
, but as you can see, for_each
, begin
, and end
are all apparently sitting in the global namespace.
Why?
#include <algorithm>
#include <iostream>
#include <vector>
int main(int argc, const char *argv[])
{
std::vector<int> v = { 1,2,3 };
// Shouldn't this need to be std::for_each?
// And std::begin and std::end??
for_each(begin(v), end(v), [](auto i){ std::cout << i << std::endl; });
return 0;
}
Command I ran to compile
$ g++ --version
g++ (GCC) 7.2.1 20170915 (Red Hat 7.2.1-2)
$ g++ what.cpp