두 번째 요소 만이 모호성을 유발합니다. 함수가 템플릿인지 여부에 관계없이 반환 유형에 따라 오버로드 할 수 없기 때문입니다.
당신이 사용할 수있는 다른 두 : 호출의 두 번째 인수를 int 인 경우
template<typename X> void func(X x, int y);
은 세 가지로 FUNC를 호출하는 경우 예를 들어 func("string", 10);
template<class X, class Y, class Z> void func(X x, Y y, Z z);
가 사용됩니다 사용됩니다 인수.
다른 답변에서 템플릿 함수 및 함수 오버로드가 섞이지 않는다고 언급하는 이유를 알 수 없습니다. 그들은 확실히하고, 호출 할 함수가 선택되는 특별한 규칙이 있습니다.
A function template can be overloaded with other function templates and with normal (non-template) functions. A normal function is not related to a function template (i.e., it is never considered to be a specialization), even if it has the same name and type as a potentially generated function template specialization.)
14.5.5 형판 비 (또는 "이하 템플릿"), 과부하 템플릿 바람직 예
template <class T> void foo(T);
void foo(int);
foo(10); //calls void foo(int)
foo(10u); //calls void foo(T) with T = unsigned
비 - 템플릿 파라미터 첫 과부하 또한이 규칙에 해당됩니다.
,보다 전문적인 일치가 선호하는 몇 가지 템플릿 사이 을 감안할 때 선택 : 당신은 표준의 같은 장에서 모든 규칙의 형식적인 설명을 찾을 수 있습니다
template <class T> void foo(T);
template <class T> void foo(T*);
int i;
int* p;
int arr[10];
foo(i); //calls first
foo(p); //calls second
foo(arr); //calls second: array decays to pointer
(기능 템플릿) 을
마지막으로 두 개 이상의 과부하가 모호한 경우가 있습니다.
template <class T> void foo(T, int);
template <class T> void foo(int, T);
foo(1, 2);
두 후보가 똑같이 특수화되어 있기 때문에이 호출은 모호합니다.
예를 들어 boost::disable_if
을 사용하여 이러한 상황을 모호하게 할 수 있습니다. 예를 들어, 우리는 지정할 수 T = INT는 다음 두 번째 과부하 과부하 후보에 포함되지 않아야 할 때 : 여기
#include <boost/utility/enable_if.hpp>
#include <boost/type_traits/is_same.hpp>
template <class T>
void foo(T x, int i);
template <class T>
typename boost::disable_if<boost::is_same<int, T> >::type
foo(int i, T x);
foo(1, 2); //calls the first
라이브러리 제 과부하 리턴 유형에 "교체 실패"를 생성 , T = int 인 경우 과부하 후보 집합에서 제거합니다.
실제로 이런 상황이 발생하는 경우는 거의 없습니다.
모든 후행하는';'문자는 무의미합니다. 그것들은 요구되지 않으며 실제로 보는 것이 더 혼란스럽게 만듭니다. – Omnifarious