foo
呼び出しに一致するものが他にまったくない場合にのみ適用できる汎用バージョンの関数を提供したいという問題があります。次のコードを変更して、よりもlast_resort::foo
一致しないようにするにはどうすればよいですか?の定義を変更する必要がなく、の引数の型を保持する解決策を見つけたいと思います 。derived::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;
}