2014-04-03 2 views
1

Implicit type conversion rules in C++ operators오퍼레이터로부터 승격 결과를 얻을 수있는 특성을 입력하십시오.

C++의 암시 적 유형 변환을 나열합니다.

문제는 그 테이블을 수행 할 수있는 유형 특성이 있습니까?

template <typename T, typename J> 
struct promotion_type 
{ 
    typedef decltype(operator+(const T&,const J&)) type; 
}; 

에 가깝다 뭔가 (안 내 질문에, 그러나 이것은 컴파일되지 않습니다 :

/home/user/source/testdir/main.cpp:97:51: error: there are no arguments to 'operator+' that depend on a template parameter, so a declaration of 'operator+' must be available [-fpermissive] 
    typedef decltype(operator+(const T&,const J&)) type; 
              ^
/home/user/source/testdir/main.cpp:97:51: note: (if you use '-fpermissive', G++ will accept your code, but allowing the use of an undeclared name is deprecated) 

)

답변

3

방법에 대한 decltype(std::declval<T>() + std::declval<J>())?

+0

흥미 롭습니다 ... 저는 std :: common_type이 충분하지 않다는 것을 확인하기 위해 사용했습니다. – IdeaHat

+0

@MadScienceDreams 유일한 차이점은 bool, char 및 short가 모두 '+'로 int로 승격되지만, T == J이면 common_type으로 승격되지 않는다는 것입니다. common_type의 레퍼런스 구현은'decltype (true? declval () : decl())'입니다. – Oktalist

관련 문제