friend templates in gcc-2.8.0 & egcs-1.0.1

Levente Farkas !spam-lfarkas@ohsh.u-szeged.hu
Sat Jan 31 02:48:00 GMT 1998


On 31-Jan-98 Alexandre Oliva wrote:
> 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;
>> }
>> --------------------

thanks for it, but
1, from where can I know things like that ?
2, if I modify the program this way, than it's no longer works on
all other unix system (Solaris, AIX) even with gcc (but "older" version
2.7.2) and I'm not the sysadm everywhere.

So is there any backward compatible solution ???
or I can't use egcs :-(
or redesign our project (in this case friend is unnecessary, but..):-(

-- Levente

---------------------------------------------------------------------
E-Mail:   Levente Farkas <!spam-lfarkas@ohsh.u-szeged.hu>
Homepage: http://www.inf.u-szeged.hu/~lfarkas/
PGP public key & Geek Code: !spam-lfarkas@anna.inf.u-szeged.hu
This message was sent by XF-Mail, Date: 31-Jan-98  Time: 11:31:58
---------------------------------------------------------------------



More information about the Gcc mailing list