How to define a friend template function of a template class outside of its declaration?
问题 If I define the friend template function inside the template class declaration, as follows, it can compile. #include <iostream> template <typename T> class X { public: X(int i) : n(i) {} private: T n; template <typename U> friend void doStuff(const X<T>& x, const U& u) { std::cout << (x.n + u) << std::endl; } }; int main() { X<int> x(1); doStuff(x, 3); return 0; } But if I move the definition of doStuff() out of the class decalaration, just after the declaration of class X<> , as follows, it