operator<<(ostream&, X) for class X nested in a class template

后端 未结 4 1408
终归单人心
终归单人心 2021-01-04 02:52

This one compiles and works like it should (non-nested template):

#include 

template class Z;

template          


        
4条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-04 03:15

    Matthieu explained the problem very well, but a simple work-around can be used in this case. You can implement the friend function inside the class with the friend declaration:

    #include 
    
    template  class Z {
    public:
      class ZZ {
        friend std::ostream& operator<< (std::ostream& os, const ZZ&) {
          return os << "ZZ!";
        }
      };
    };
    
    
    int main () {
      Z::ZZ zz;
      std::cout << zz << std::endl;
    }
    

提交回复
热议问题