2

This answer explains the behaviour of the following program:

template<typename A, typename B = int >
struct FirstWins {
    static constexpr int i = 1;
};
template<typename A>
struct FirstWins<A, float/* anything different from int */ > {
    static constexpr int i = 2;
};

template<typename A, typename B = int >
struct SecondWins {
    static constexpr int i = 1;
};
template<typename A>
struct SecondWins<A, int > {
    static constexpr int i = 2;
};

int main()
{
    typedef void Whatever_t;
    cout << FirstWins < Whatever_t >::i << endl;  // prints 1
    cout << SecondWins< Whatever_t >::i << endl;  // prints 2
    return 0;
}

However, I cannot find an actual reference describing explicitly this behaviour and thus confirming the answer.

I could not find on cppreference.com a sentence confirming that explicit template arguments are preferred over default ones.

I suspect that this is not really the rule. The rule is that whenever there is a partial template specialization matching the template arguments, that specialization is always chosen over the instantiation of the primary template. Is this correct? (in this case, the docs somehow explain this rule, but again not explicitly).

Holt
  • 36,600
  • 7
  • 92
  • 139
L. Bruce
  • 170
  • 7

1 Answers1

0
template<typename A, typename B = int >
struct S {
//...
};

Can be seen as

template<typename A, typename B = int >
struct S;
// So S<A> is S<A, int>

// Primary template
template<typename A, typename B>
struct S
{
   //...
};

// Possible specialization as
template<typename A>
struct S<A, int>
{
   //...
};

then, it is more clear which instantiation to use.

Jarod42
  • 203,559
  • 14
  • 181
  • 302