2016-11-10 4 views
1

매개 변수 T_rhs 모두 기본 유형이 아니므로 내 코드는 프리미티브 유형을 반환하는 cast_to_primitive() 함수를 구현하는 클래스를 나타내도록 설계되었습니다. 이 첫 번째 경우에는 T_res 유형이 decltype(std::declval<T_rhs>().cast_to_primitive()) 유형의 요소로 decltype(std::declval<T_lhs>().cast_to_primitive()) 유형의 요소로 나눈 유형을 갖기를 원합니다. T_lhs 또는 T_rhs 하나는 원시 타입 (또는 둘 다) 상기 제 2에서는 유형 정의는, 내가 <code>A</code>의 템플릿 매개 변수 <code>T_lhs</code> 및 <code>T_rhs</code>에 따라 유형 <code>T_res</code>을 정의 할 템플릿 클래스 <code>A</code> 감안할 때

, I는 입력 T_rhs의 요소에 의해 입력 T_lhs의 엘리먼트를 분할함으로써 얻어지는 형태를 가질 T_res 원한다. 예를 들어, T_lhs이 기본 유형 인 경우 내 코드는 T_rhsT_lhs 유형의 요소에 내재적으로 형변환 될 수 있도록 설계되었습니다. T_rhs이 프리미티브 인 경우에도 마찬가지입니다.

불행히도 위의 코드는 컴파일되지 않습니다. 오류 :

error: template non-type parameter has a different type 'std::enable_if_t<(lhs_is_fundamental || rhs_is_fundamental)> *' (aka 'typename enable_if<(lhs_is_fundamental || rhs_is_fundamental), void>::type *') in template redeclaration 
      std::enable_if_t<(lhs_is_fundamental || rhs_is_fundamental)>* = nullptr > 
                      ^
note: previous non-type template parameter with type 'std::enable_if_t<(!lhs_is_fundamental && !rhs_is_fundamental)> *' (aka 'typename enable_if<(!lhs_is_fundamental && !rhs_is_fundamental), void>::type *') is here 
      std::enable_if_t<(!lhs_is_fundamental && !rhs_is_fundamental)>* = nullptr > 
                      ^

문제를 해결하는 데 도움을 줄 수있는 사람이 있습니까? std::conditional 인스턴스화 한 후

+0

' std :: conditional_t ​​<..>'도움이 될 수 있습니다. – Jarod42

+0

A 클래스의 목적이 무엇인지 알면 도움이됩니다. – Rerito

답변

3

이 대략 해결책이 될 때까지 cast_to_primitive에 부문/콜의 인스턴스를 지연 헬퍼 클래스와 함께 std::conditional을 사용하는 것입니다 Using std::conditional_t to define a class' typedef in dependence of its template parameter

에있는 동일한 문제입니다 :

#include <type_traits> 

template<class T1, class T2> 
struct A 
{ 
    template<class T=T1, class U=T2> 
    struct cast_to_primitive_t {using type=decltype(std::declval<T>().cast_to_primitive()/std::declval<U>().cast_to_primitive());}; 
    template<class T=T1, class U=T2> 
    struct primitive_div_t {using type=decltype(std::declval<T>()/std::declval<U>());}; 

    using T_res = typename std::conditional_t<std::is_fundamental<T1>{} && std::is_fundamental<T2>{}, primitive_div_t<>, cast_to_primitive_t<>>::type; 
};