하위 유형을 정의하는 템플릿 클래스가 있습니다. 이진 코드 operator+
을 템플릿 함수로 정의하려고하지만 컴파일러에서 operator+
의 템플릿 버전을 확인할 수 없습니다.연산자 클래스 + 하위 클래스 용
#include <iostream>
template<typename other_type>
struct c {
c(other_type v) : cs(v) {}
struct subtype { subtype(other_type v) : val(v) {} other_type val; } cs;
};
template<typename other_type>
typename c<other_type>::subtype operator+(const typename c<other_type>::subtype& left,
const typename c<other_type>::subtype& right)
{ return typename c<other_type>::subtype(left.val + right.val); }
// This one works
// c<int>::subtype operator+(const c<int>::subtype& left,
// const c<int>::subtype& right)
// { return c<int>::subtype(left.val + right.val); }
int main()
{
c<int> c1 = 1;
c<int> c2 = 2;
c<int>::subtype cs3 = c1.cs + c2.cs;
std::cerr << cs3.val << std::endl;
}
가 나는 operator+<int>
대신 operator+
검색 그래서 컴파일러 (g ++ 4.3) 템플릿 유형을 추측 할 수 없기 때문에 이유라고 생각합니다.
그 이유는 무엇입니까? 어떤 우아한 해결책을 제안 할 수 있습니까?
에 코드를 변경할 수는 C++ 표준 14.8.2.4/4에 따라 여기 문맥을 nondeduced 점이다. –