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: something wrong with template instantiation mechanics


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


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