When trying to get to grips with std::ostream_iterator I have come up with the following code which doesn't compile (under gcc 5.3 or clang 3.6).
#include <iostream>
#include <iterator>
namespace temp {
struct Point {
int x;
};
}
//namespace temp {
//namespace std {
std::ostream& operator<<(std::ostream& s, temp::Point p) {
return s << p.x;
}
//}
int main(int argc, char** argv) {
temp::Point p{1};
std::ostream_iterator{std::cout} = p;
//std::cout << p;
std::cout << std::endl;
return 0;
}
When operator<< is in global scope, compilation throws up a host of template instantiation errors.
However, std::cout << p works fine. And, if operator<< is declare in namespace temp or namespace std, the code compiles and runs as one would expect.
My question is why does a global operator<< not work?