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;
}