template friends

Stefan Schwarzer sts@ica1.uni-stuttgart.de
Thu Apr 30 15:41:00 GMT 1998


//
// Dear gcc/egcs-developers,
//
// according to  14.5.3 (1 and 2, temp.friend) one should be able to declare  
// a function template as friend of a template (1) or non-template class (2). 
// The appended code (I know, it's a bit artificial) fails to compile with 
// gnu/egcs (compiles fine with cxx-6.1/KCC 3.2+):  
//
//  --- gcc 2.8.1:
//
//22:38 lynx_sts:~/bugs/gcc> g++ t.cc
//t.cc:15: warning: friend declaration `class ostream & operator <<(class ostream &, const struct B<S> &)'
//t.cc:15: warning:   will not be treated as a template instantiation
//t.cc:15: warning:   unless you compile with -fguiding-decls
//t.cc:15: warning:   or add <> after the function name
//t.cc:15: invalid member template declaration
//t.cc: In function `class ostream & operator <<<int>(class ostream &, const struct B<int> &)':
//t.cc:22: member `a_' is a private member of class `B<int>'
//22:43 lynx_sts:~/bugs/gcc> g++ -v
//Reading specs from /local/gcc-2.8.1/lib/gcc-lib/alphaev56-dec-osf4.0c/2.8.1/specs
//gcc version 2.8.1
//
//  --- egcs 1.0.3 prerelease:
//
//22:47 boa_sts:~/bugs/gcc> /local/egcs-1.0.3-prerelease/bin/g++ -v
//Reading specs from /local/egcs-1.0.3-prerelease/lib/gcc-lib/sparc-sun-solaris2.5.1/egcs-2.90.28/specs
//gcc version egcs-2.90.28 980423 (egcs-1.0.3 prerelease)
// 
//22:47 boa_sts:~/bugs/gcc> /local/egcs-1.0.3-prerelease/bin/g++ t.cc
//t.cc:15: warning: friend declaration `class ostream & operator <<(class ostream &, const struct B<S> &)'
//t.cc:15: warning:   will not be treated as a template instantiation
//t.cc:15: warning:   unless you compile with -fguiding-decls
//t.cc:15: warning:   or add <> after the function name
//t.cc:15: invalid member template declaration
//t.cc: In function `class ostream & operator <<<int>(class ostream &, const struct B<int> &)':
//t.cc:22: member `a_' is a private member of class `B<int>'
//
// Best regards, 
//
//Stefan Schwarzer                office:  +49-(0)711-685-7606   fax: x-3658 
//Uni Stuttgart, ICA 1            e-mail:          sts@ica1.uni-stuttgart.de
//Pfaffenwaldring 27              pgp public key available on request/finger 
//70569 Stuttgart, Germany             http://www.ica1.uni-stuttgart.de/~sts
//
//
#include <iostream.h>

template<class T> class B;
 
template <class S> ostream & 
operator<<( ostream &o, const B<S>& );


template <class T> 
struct B { 
  B(T a): a_(a){} 

  template<class S>
  friend ostream & operator<< (ostream &o, const B<S> &);
private:
  T a_; 
};

template <class S> ostream & 
operator<<( ostream &o, const B<S>& a){
  return o << "B<S>.a_ = " << a.a_ << "\n"; 
}


main() 
{
  B<int>  d(1234); 
  cout << d;

}







More information about the Gcc-bugs mailing list