This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: friend templates in gcc-2.8.0 & egcs-1.0.1


This should be in the FAQ list.  Jeff, would you put this in?

Levente Farkas writes:

> n_vector.h:69: warning: friend declaration `class N_Vector<T> operator *(const
> T &, const class N_Vector<T> &)'
> n_vector.h:69: warning:   will not be treated as a template instantiation
> n_vector.h:69: warning:   unless you compile with -fguiding-decls
> n_vector.h:69: warning:   or add <> after the function name

In order to make a specialization of a template function a friend of a
(possibly template) class, you must explicitly state that the friend
function is a template, by appending angle brackets to its name, and
this template function must have been declared already.  An error in
the last public comment draft of the ANSI/ISO C++ Standard has led
people to believe that was not necessary, but it is, and it was fixed
in the final version of the Standard.

Which means you must declare operator* as a template function, before
it is referred to, but then you must have declared the class template
too, as follows:

template <class T> class N_Vector;
template <class T> inline N_Vector<T> operator*(const T&, const N_Vector<T>&);

Now you may define N_Vector:

> template <class T>
> class N_Vector : public Vector<T>
> {
>   public :
>   ...
>  N_Vector<T>   operator * (const T &) const;
but you must add angle brackets when you refer to a template function:
>  //friend N_Vector<T> operator *
   friend N_Vector<T> operator * <>
>  (const T &, const N_Vector<T> &);
> }
>  ...

and then you can define the template function:

> template <class T>
> inline N_Vector<T> 
> operator *(const T & Scalar, const N_Vector<T> & V)
> {
>   return V * Scalar;
> }
> --------------------

-- 
Alexandre Oliva
mailto:oliva@dcc.unicamp.br mailto:aoliva@acm.org
http://www.dcc.unicamp.br/~oliva
Universidade Estadual de Campinas, SP, Brasil


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]