This is the mail archive of the gcc-help@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]
Other format: [Raw text]

Template specialisation


The following example seems to instance the templated function incorrectly:

class test
{
public:
 float operator[]( int index )
 {
  return testFloat[index];
 }
private:
 float testFloat[3];
};

template < class typeA > float
operator*(
 typeA a,
 float b
)
{
 return a[0] * b;
}

template < class typeB > float
operator*(
 float a,
 typeB b
)
{
 return a * b[0];
}

template < class typeA, class typeB > float
operator*(
 typeA a,
 typeB b
)
{
 return a[0] * b[0];
}

int main( void )
{
 test aTest;
 float bTest;
 float result;

 result = aTest * bTest;
 result = bTest * aTest;

 return 0;
}

with the errors:

test.cpp:31: `float operator*(float, typeB) [with typeB = float]' must have
an argument of class or enumerated type
test.cpp:22: `float operator*(typeA, float) [with typeA = float]' must have
an argument of class or enumerated type

Surely if the operation is float * class the the more specialised template
is the float, typeB version, not the typeA, float version?

Am I doing something stupid, as I though gcc support partial template
specialisation.

Thanks for any help,
Nick Newson.


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