template friend functions of template class
问题 I have the following template class and template function which intends to access the class' private data member: #include <iostream> template<class T> class MyVar { int x; }; template<class T> void printVar(const MyVar<T>& var) { std::cout << var.x << std::endl; } template<class T> void scanVar(MyVar<T>& var) { std::cin >> var.x; } struct Foo {}; int main(void) { MyVar<Foo> a; scanVar(a); printVar(a); return 0; } To declare the two functions as MyVar<T> 's friend functions, I've tried the