I have a problem where I'd like to provide a generic version of a function foo
which may only be applied when there is absolutely no other match for an invocation. How can I modify the following code such that last_resort::foo
is a worse match for derived::type
than base::foo
? I'd like to find a solution which does not involve modifying the definition of bar
and which would preserve the type of the argument of 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;
}