나는 다소 단순하다. 상상해 보니 CRTP에 대한 질문이지만 대답을 찾을 수없는 것 같다. 아마, 그것은 매우 간단하기 때문에, 아무도 그것을 요구할 생각을하지 않았습니다. 나는이 개념을 처음 사용합니다. 너무 웃지 마세요.). 내가 잘못된 typedef typename _THelpee::T Th;
을 언급하는 경우typedef의 CRTP 관련 컴파일러 오류
template< typename tT >
struct TBase
{
typedef tT T;
};
template< typename tTBase >
struct TTraitsBase
{
typedef typename tTBase::T T;
};
template< typename tTHelpee, typename tTTraits >
struct THelper
{
typedef typename tTTraits::T T;
typedef typename tTHelpee::T Th; /* This generates a compiler error:
'T' not being a member of TDerived<tT> */
T Foo(void)
{
return static_cast< tTHelpee* > (this)->mVal;
}
};
template< typename tT >
struct TDerived : TBase<tT> , THelper< TDerived<tT> , TTraitsBase< TBase<tT> > >
{
using TBase<tT>::T;
T mVal;
};
int main()
{
TDerived<int>::T lTmp = -1;
TDerived<int> lObj;
lObj.mVal = -1;
std::cout << lObj.Foo() << std::endl;
return 0;
}
모든 컴파일 : 여기
은 (STL 컨테이너와 유사한 무언가를하기위한 시도의 그것의 종류) 코드입니다. 그리고 저를 혼란스럽게합니다 : 컴파일러가
typedef typename _THelpee::T Th;
을 좋아하지 않는다면, 왜 그것이
static_cast< _THelpee* > (this)->mVal
을 통과하게합니까? 나는 그것이
TDerived
을 인스턴스화 할 때
THelper
을 인스턴스화 할 수 없다는 것과 관련이 있지만 확실한 이해가 없다고 가정합니다. 누군가, 제발, 여기에서 일어나는 일에 대한 간략한 설명 및/또는 참고 문헌을 줄 수 있습니까? 고맙습니다.
편집 : '_T'접두어가 제거되었습니다.
[_ 및 대문자로 시작하는 식별자는 예약되어 있습니다] (http://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-ac) -identifier/228797 # 228797). – Flexo
자신의 식별자에 밑줄 - 자본을 사용하지 마십시오. 금지되어 있습니다. –