2

In the code below the compiler chooses template overload B for Foo<float> and overload A for all other types e.g. Foo<char>. This seems to depend on whether the second template argument in overload B matches the specified default of void for that template argument in the initial definition of Foo.

I don't understand the mechanism behind this. Please explain this sorcery.

template <typename T>
struct Bar
{
    using type = int;
};

template <>
struct Bar<float>
{
    using type = void;
};

// overload A
template <typename T1, typename T2=void>
struct Foo
{
    using type = int;
};

// overload B
template <typename T>
struct Foo<T, typename Bar<T>::type>
{
    using type = float;
};

int main()
{
    [[maybe_unused]] Foo<float>::type foo1 {2.3};    // compiler picks overload B
    [[maybe_unused]] Foo<char>::type foo2 {2};       // compiler picks overload A

    return 0;
} 
Ziffusion
  • 8,779
  • 4
  • 29
  • 57
  • They're not overloads, `A` is a primary template, and `B` is a specialization. Basically, if the arguments for the specialization don't match the *default* parameters in the primary, the primary is selected. – cigien Sep 17 '20 at 03:06

0 Answers0