SHARE
TWEET

Template non-member friend

a guest Nov 9th, 2010 32 Never
  1. #include <iostream>
  2.  
  3. template <typename T> class Matrix;
  4. template <typename T> std::ostream& operator<<(
  5.     std::ostream& ostr, const Matrix<T>& m);
  6.  
  7. template <typename T>
  8. class Matrix {
  9. private:
  10.     int _rank;
  11.     friend std::ostream& operator<< <> (
  12.       std::ostream& ostr, const Matrix& m );
  13. };
  14.  
  15. template <typename T> std::ostream& operator<<(
  16.     std::ostream& ostr, const Matrix<T>& m )
  17. {
  18.     (void) m._rank; // test friendship
  19.     return ostr;
  20. }
  21.  
  22. void f(const Matrix<int>& m)
  23. {
  24.     std::cout << m;
  25. }
RAW Paste Data
Top