This is a pretty short snippet that just won’t compile with g++ 4.7.1 (it won’t compile either with gcc 4.6.3 by the way).
#include <iostream>
template<typename T>
struct Foo
{
template<typename U>
friend std::ostream& operator<<(Foo&, U&);
};
template<typename T, typename U>
std::ostream& operator<<(Foo<T> foo, U& u)
{
std::cout << u;
return std::cout;
}
int main()
{
Foo<int> f;
f << "bar";
return 0;
}
And this is what gcc 4.7.1 outputs (4.6.3 says almost the same thing).
/tmp/ccNWJW6X.o: In function
main': main.cpp:(.text+0x15): undefined reference to
std::basic_ostream >& operator<< (Foo&, char const (&) [4])' collect2: ld returned 1 exit status
Anyone could explain why?
EDIT
I also tried with clang 3.1, and it says exactly the same thing.