Re: About the instantiation of a template class
On Oct 12, 4:31 am, Adam Nielsen <adam.niel...@remove.this.uq.edu.au>
wrote:
void operator+(A<T> lhs, A<T> rhs) {
This function takes two objects of the A class.
This is not a function, but a template. It only becomes a
function when instantiated.
obj + t;
But here you're calling operator+ with one A object and one int.
Here, you're asking the compiler to deduce the types for the
above template, and instantiate it using the deduced types.
1. error C2784: 'void operator +(A<T>,A<T>)' : could not deduce
template argument for 'A<T>' from 'int'
The compiler is complaining that it doesn't know how to convert an int
into an A<something> object.
No. The compiler knows how to convert an int into an A<int>
object, which is what is wanted. The problem here is that the
rules for type deduction do not allow the compiler to deduce the
arguments for the operator+ template, so it cannot instantiate
the function.
If you used two A objects it would work:
A<int> obj(5);
A<int> obj2(5);
obj + obj2;
You could also rewrite your + operator so that the object
doesn't need to be an instance of A<something>:
void operator+(A<T> lhs, T rhs)
Or alternatively it may be possible to define an operator that
allows an int to be converted into an A<int> - sorry, I'm not
sure of the exact syntax. (I would've thought the constructor
would be called implicitly though - perhaps someone can
enlighten us.)
The usual solution here would be something along the lines of:
template< typename T >
class A
: public Operators< A< T > >
, public MixedOperators< A< T >, T >
{
public:
// ...
A& operator+=( A const& other ) ;
} ;
with the usual definitions for Operators and MixedOperators:
template< typename T >
class Operators
{
public:
friend T operator+( T const& lhs, T const& rhs )
{
T result( lhs ) ;
result += rhs ;
return result ;
}
// And so on for the other operators...
} ;
template< typename T1, typename T2 >
class MixedOperators
{
public:
friend T1 operator+( T1 const& lhs, T2 const& rhs )
{
T1 result( lhs ) ;
result += rhs ; // Note that here, template
// type deduction isn't needed,
// so the compiler will find
// any necessary conversions.
return result ;
}
// And so on for the other operators...
friend T1 operator+( T2 const& lhs, T1 const& rhs )
{
T1 result( rhs ) ;
result += lhs ;
return result ;
}
// And so on for the other commutative operators...
} ;
(Note that in this solution, there are no function
templates:-).)
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34