2010-08-17 4 views
0

아무도 이것을 설명 할 수 있습니까?구성원 템플릿, Statement ISO C++ Standard?

"Overload resolution and partial ordering are used to select the best 
    conversion function among multiple template conversion functions and 
    or non-template conversion functions." 

프로그램으로 설명해주십시오 ..... 문 8

+0

실수로 작성해야합니다. 발췌 부분은 분명히 영어가 아닙니다. – Borealid

+1

어떤 부분이 당신을 혼란스럽게합니까? "All it it"은 계산되지 않습니다 :) –

답변

3
struct S{ 
    template<class T> operator T(){return T();} 
    operator int(){return 0;} 
}; 

int main(){ 
    S s; 
    int xi = s; // both template and non template are viable. Overload res chooses non tmpl 
    char xc = s; // both template and non template are viable. Overload res chooses tmpl 
} 

편집 점, ISO C++ 표준 14.5.2 섹션입니다 : 첫번째 코멘트 후

struct B{ 
    operator int(){return 0;} 
}; 

struct S : B{ 
    template<class T> operator T(){return T();} 
    operator int(){return 0;} 
    template<class T> operator T*(){return T();} 
}; 

int main(){ 
    S s; 
    int xi = s; // Overload reslution between operator T and operator int 
    char xc = s; // Overload resolution between operator T and operator int 
    int *pi = s; // Partial ordering involved between operator T() and operator T*() 
} 

위의 코드는 템플릿/비 템플릿 모두가 관련되어있는 경우 부분 정렬 및 과부하 해결을 보여줍니다.

+0

예, 두 번째 프로그램은 연산자 int를 기본 클래스로 옮겼습니다. 그 코드 스 니펫은 오버로드를 포함하지 않았습니다 .... 오버로드는 기본/파생 클래스 범위 사이에 있지 않습니다. – Chubsdad

+0

간단히 말하면, 연산자 T()와 연산자 T *()는 's'를 'int *'로 변환하는 실행 가능한 함수라는 것을 의미합니다. 그러나 C++ 규칙에 따라 연산자 T *()는 연산자 T보다 특수화 된 것으로 간주됩니다. David-Vandevoorde의 "C++ Templates"섹션 12.2.2/3에 대한 참조를 좋아합니다. 다시 말하면 매우 단순한 뷰를 취하면 연산자 T *()를 적용 할 수있는 변환이 연산자 T()가 적용될 수있는 변환의 하위 집합이므로 연산자 T *()가 연산자 T()보다 더 전문화되어 있어야합니다. – Chubsdad

+0

고맙습니다 ... 명확한 답변 –