2010-05-19 3 views
2

다음 코드는 컴파일되지 않습니다. 문제는 T::rank에 액세스 할 수 없거나 상위 템플릿에서 초기화되지 않은 것입니다.C++ CRTP (템플릿 패턴) 질문

문제점을 정확히 알 수 있습니까? 은 명시 적으로 유일한 길을 통과하고 있습니까? 또는 직접 텐서 클래스를 쿼리하는 방법이 있습니까?

그러나

+0

Visual Studio에서 발견 된이 버그와 관련이 있습니까? http://connect.microsoft.com/VisualStudio/feedback/details/354162/seemingly-valid-dependant-types-in-nested-template-not-accepted –

+0

@kirill g ++ 4.3. 인스턴스화가있는 마지막 줄을 추가하는 것을 잊었습니다. 지금 고정 – Anycorn

+0

@evan 그것은 실제로 비슷하게 보입니다. – Anycorn

답변

2

CRTP에서 기본 클래스 : 여기

는 GCC 3.4.2에 오류가 템플릿은 멤버 함수 본문 (정의)이 선언 후에 오랫동안 인스턴스화되지 않는다는 사실을 이용합니다. 귀하의 코드에서 기본 클래스는 불완전한 유형에 따라 다릅니다.

+0

이 답변은 실제로 맞습니다. 인스턴스화 시점이 'tensors <4>'의 인스턴스화 지점보다 먼저 있기 때문에':: rank'에 대한 SFINAE 실패가 발생합니다 (':: rank' 멤버가 아직 선언되지 않았기 때문에). 관련 문제에 대한 정보는 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#287을 참조하십시오. –

2

내가 여기에 무한 재귀보고 뿐이야 자기 교육을위한 템플릿 인스턴스화의 역학에 관심이 있어요, 내가 해결 방법을 알고 당신에게

#include <boost/utility/enable_if.hpp> 

template<class T, // size_t N, 
     class enable = void> 
struct tensor_operator; 

// template<class T, size_t N> 
template<class T> 
struct tensor_operator<T, typename boost::enable_if_c< T::rank == 4>::type > { 
    tensor_operator(T &tensor) : tensor_(tensor) {} 
    T& operator()(int i,int j,int k,int l) { 
     return tensor_.layout.element_at(i, j, k, l); 
    } 
    T &tensor_; 
}; 

template<size_t N, typename T = double> 
// struct tensor : tensor_operator<tensor<N,T>, N> { 
struct tensor : tensor_operator<tensor<N,T> > { 
    static const size_t rank = N; 
}; 

tensor <4> D; // compiler attempts to instantiate undefined template, not specialization 

감사? tensor_operator< tensor<N,T> >

  • tensor_operator< tensor<N,T> >은 내가 Base를 인스턴스화 여부를 결정하기 위해 Derived 클래스 속성을 사용하는 상황을 기억하지 않습니다 tensor<N,T>
  • 에 따라 달라집니다에

    • tensor<N,T>가 달려 있지만 보인다 내게 이렇게하면 무한 재귀가 발생합니다.

      In instantiation of `tensor<4ul, double>': 
      41: instantiated from here 
      33: error: invalid use of undefined type 
                `struct tensor_operator<tensor<4ul, double>, void>' 
      19: error: declaration of `struct tensor_operator<tensor<4ul, double>, void>' 
      
      문제는 여기 tensor_operator<N,T>의 인스턴스가 tensor_operator<N,T>의 인스턴스에 의존하는 것 같다

      ...

    +1

    재귀 문제라고 생각하지 않습니다. 부모 템플릿이 인스턴스화 된 후에 순위가 지정 되었기 때문에 아마도 그렇게되지 않았을까? – Anycorn