2017-12-02 20 views
1
template <class Derived> 
struct Base { 
    typedef typename Derived::T T; 
}; 

template <typename T_> 
struct Impl : public Base<Impl<T_>> { 
    typedef T_ T; 
}; 

이 초기화 때, 나는 'IMPL'그래서기본 클래스에서 파생 된 typename을 얻는 방법은 무엇입니까?

, 어떻게 얻을 수있는 유형 이름에 'T'라는 이름

어떤 유형의 오류가 기본 클래스에서 파생 된 정의있어?

+1

[템플릿 및 "typename"키워드를 어디에 왜 넣어야합니까?] (https://stackoverflow.com/questions/610245/where-and-why-do-i-have -to-put-the-template-and-typename-keywords) – user0042

+0

'typename'키워드 자체에 관한 것이 아니라 typename 가시성에 관한 것입니다. –

+0

필자는 일종의 원형 정의라고 말할 것입니다. Impl에서 T를 두 번 정의하면,'struct Impl : public Base {'? – nsubiron

답변

0

완벽하지는 않지만 작동하지만 방법을 찾아보십시오. 특색 클래스에서, 나는 아직 정의되지

typedef typename Impl<T_>::T T; 

을 쓸 수 없기 때문에

template <class T> 
struct traits; 

template <class Derived> 
struct Base { 
    typedef typename traits<Derived>::T T; 
}; 

template <typename T_> 
struct Impl : public Base<Impl<T_>> { 
    typedef T_ T; 
}; 

template <typename T_> 
struct traits<Impl<T_>> { 
    typedef T_ T; 
}; 

그것은 완벽하지 않습니다.