나는 호출을위한 다른 일치가 전혀 없을 때만 적용될 수있는 함수 foo
의 일반 버전을 제공하고자하는 곳에 문제가 있습니다. last_resort::foo
이 derived::type
보다 좋지 않은 코드 인 base::foo
을 수정하려면 어떻게해야합니까? bar
의 정의를 수정하지 않고 last_resort::foo
의 인수 유형을 보존하는 솔루션을 찾고 싶습니다.ADL 동안 함수 템플릿의 우선 순위를 낮추는 방법은 무엇입니까?
#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;
}
사실입니다. 그러나 foo에 기본값으로 추가 매개 변수를 추가하여 "숨겨진"변환을 도입 할 수있는 방법이 있는지 궁금합니다. –
작동하지 않을 것이라고 생각합니다. 당신이 얻을 수있는 "최선"이라고 말할 수있는 것은 모호하므로 컴파일이 실패합니다. 하지만 어쩌면 뭔가 빠져있는 것일 수도 있습니다. – sellibitze
해결책은'''last_resort :: foo'''의 리턴 타입을''disable_if_foo_exists :: type''과 같이 꾸미는 것으로 보인다. SFINAE를 사용하여 자유로운''foo '''기능. 만약 존재한다면,''last_resort :: foo'''는 오버로드 세트에서 제거 될 것입니다. –