something wrong with template instantiation mechanics

Alexandre Oliva oliva@dcc.unicamp.br
Fri Oct 10 20:46:00 GMT 1997


Oleg Krivosheev writes:

> template <class T> class X {

Here, you declare a non-template operator+ :

>     friend X<T> operator+( const X<T>&, const X<T>& );

> };

And here, you declare a define a template one:

> template <class T> X<T>
> operator+( const X<T>& a, const X<T>& b ) {
>   return X<T>( a.t_ + b.t_ );
> }

And here, overload resolution chooses the non-template one.

>   x = x+y;

> as one can see, op+ is not instantiated.

It shouldn't, since it is not used at all.  The non-template operator+
you use, OTOH, is not defined.  You should have declared the template
version of the function friend, like this:

template <typename T>
class X;
template <typename T>
X<T> operator+(const X<T>&, const X<T>&);

template <typename T>
class X {
// note the angle brackets:
//                     vv
  friend X<T> operator+<>(const X<T>&, const X<T>&);
};

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



More information about the Gcc mailing list