0

If I have the following template methods:

template< typename T >
void function( T a )
{
    cout<<"Template T"<<endl;
}

template< typename T >
void function( int a )
{
    cout<<"Function taking Int"<<endl;
}

On calling function< float >( 2.5 ), the second function gets called outputting: Function taking Int.

If I try to instantiate the functions for the float type, I get

void function( float a );

And

void function( int a );

From which the first one seems to be the better match for the double type( arg 2.5 ) that I am passing.

Then, why does the program chooses the second method?

sajas
  • 1,599
  • 1
  • 17
  • 39
  • 4
    `2.5` is a `double`. `double` to `float` and `double` to `int` are equally bad. Then...partial ordering kicks in and decides that the second is more specialized than the first, so that one gets called. – T.C. Oct 25 '16 at 03:40
  • @T.C. Nice answer! – songyuanyao Oct 25 '16 at 03:44
  • @T.C. Thanks for your answer. – sajas Oct 25 '16 at 04:28
  • @songyuanyao Nah, an actual answer would explain exactly how that partial ordering works. But the rules in this area have plenty of recent changes (some/most not yet implemented in any compiler) and I'm not exactly feeling like doing standard archaeology. – T.C. Oct 25 '16 at 18:26
  • @T.C. Yes, the actual rule might be complex; it's still important to be aware that the partial ordering rule is taking effect here. – songyuanyao Oct 26 '16 at 00:35

0 Answers0