Устранение неоднозначности в перегруженном шаблоне функции с помощью `std :: enable_if` и невосстановленного контекста
Рассмотрим следующий код:
template <typename T>
struct dependent_type
{
using type = T;
};
template <typename T>
auto foo(T) -> std::enable_if_t<std::is_same<T, int>{}>
{
std::cout << "a\n";
}
template<typename T>
void foo(typename dependent_type<T>::type)
{
std::cout << "b\n";
}
Первая перегрузкаfoo
можно вывестиT
от его вызова.
Вторая перегрузкаfoo
этоне выводимый контекст.
int main()
{
foo<int>( 1 ); // prints "b"
foo<double>( 1.0 ); // prints "b"
foo( 1 ); // prints "a"
}
Почемуfoo<int>( 1 )
вывести «б», а не «а»?