У меня проблема, когда я хотел бы предоставить общую версию функции foo
, которая может применяться только тогда, когда нет абсолютно никакого другого совпадения для призыв. Как я могу изменить следующий код так, чтобы last_resort :: foo
хуже соответствовал производному :: type
, чем base :: foo
? Я хотел бы найти решение, которое не требует изменения определения bar
и сохранит тип аргумента last_resort :: foo
.
#include <iostream>
namespace last_resort
{
template<typename T> void foo(T)
{
std::cout << "last_resort::foo" << std::endl;
}
}
template<typename T> void bar(T)
{
using last_resort::foo;
foo(T());
}
namespace unrelated
{
struct type {};
}
namespace base
{
struct type {};
void foo(type)
{
std::cout << "base::foo" << std::endl;
}
}
namespace derived
{
struct type : base::type {};
}
int main()
{
bar(unrelated::type()); // calls last_resort::foo
bar(base::type()); // calls base::foo
bar(derived::type()); // should call base::foo, but calls last_resort::foo instead
return 0;
}