Friday, April 17, 2009

Template Specialization and GCCE compiler

An interesting point about template specialization on Symbian.
GCCE does not follow §13.5.1 rule of C++ standard, the order of specializations.

Look at the follow code:

template< class T > class MyComplex { T i, j; };

template< class T > T MySqrt( T );
template< class T > MyComplex< T > MySqrt( MyComplex< T > );
double MySqrt( double );

template< class T > void MySimpleFoo ( T& );
template< class T > void MySimpleFoo ( const T& );

template< class T1, class T > void MyFoo2 ( T1 t1, T& );
template< class T1, class T > void MyFoo2 ( T1 t1, const T& );

// The problem is lying at the following 2 templates
template< class T1, class T > T1 MyFoo ( T& );
template< class T1, class T > T1 MyFoo ( const T& );

void f ( MyComplex< double > z )
{

int i = 0;
const int ci = 3;

MySqrt( 2 );
MySqrt( 2.0 );
MySqrt( z );

MySimpleFoo( 2 );
MySimpleFoo( i );
MySimpleFoo( ci );

char ch;

MyFoo2( ch, 2 );
MyFoo2( ch, i );
MyFoo2( ch, ci );

MyFoo2< char >( ch, 2 );
MyFoo2< char >( ch, i );
MyFoo2< char >( ch, ci ); // MyFoo2(..) does not alert the compiler
// for ambiguious. Now look at MyFoo(..)

MyFoo< char >( 2 );
MyFoo< char >( i );
MyFoo< char,int >( ci ); // This is ok.
// But the call below will not work

// MyFoo< char >( ci ); // doesn't work with Symbian GCCE
// the compiler will complain that the functions ambiguious,
// it cannot decide whether it should take MyFoo ( T& ) or
// MyFoo ( const T& )
}


---------------
"Trying to outsmart a compiler defeats much of the purpose of using one." Kernighan & Plauger, The Elements of Programming Style