template argument deduction을 방지하는 depedent_type
구조체를 도입 할 수 있습니다. 귀하의 예제에서
template <typename T>
struct dependent_type
{
using type = T;
};
struct A
{
void foo(int i) { /*...*/ };
template<typename T> void foo(typename dependent_type<T>::type t) { /*...*/ }
}
: (.이 동작은
cppreference >template argument deduction >non-deduced contexts에 설명)
a.foo<int>(1); // calls the template
a.foo<double>(1.0); // calls the template
a.foo(1); // calls non-templated method
a.foo(1.0); // calls non-templated method (implicit conversion)
wandbox example
당신이 a.foo(1.0)
제왕을 확인하려면
template <typename T>
auto foo(T) -> std::enable_if_t<std::is_same<T, int>{}> { }
이 기술은 foo
의 위의 과부하는 int
인수 걸릴 수 있습니다 : ompilation 오류가, 첫 번째 과부하 제한 할 필요가 암시 적 변환 를 (예를 들어, float
~ int
)은 허용되지 않습니다. 이것이 당신이 원하는 것이 아니라면, TemplateRex의 대답을 고려하십시오.
wandbox example
(a.foo<int>(1)
를 호출 할 때 내가 안내 기본 규칙을 잘 모르겠어요으로 위의 제한된 기능으로, 두 개의 오버로드를 사이에 호기심 상호 작용이 나는. asked a question about it있다.)
지금까지 당신이 당신이 원하지 않는 명시 적으로 과부하를 삭제하는 것입니다 원하는 것을 할 수있는 간단한 방법으로
이것이 필요합니까? 함수를 오버로딩하는 대신'int'에 대한 템플릿을 전문화하지 않는 이유는 무엇입니까? –
@ChristianHackl 그냥'foo (double) = delete'을해라. 내 대답을 보라. – TemplateRex