2010-05-30 62 views
2

我有一個不能用g ++ 4.2.1編譯的CRTP,可能是因爲派生類本身就是一個模板?有誰知道爲什麼這不起作用,或者更好的是,如何使它工作?示例代碼和編譯器錯誤如下。CRTP中的模板派生類(奇怪的循環模板模式)

來源:foo.c的

#include <iostream> 

using namespace std; 

template<typename X, typename D> struct foo; 

template<typename X> struct bar : foo<X,bar<X> > 
{ 
    X evaluate() { return static_cast<X>(5.3); } 
}; 

template<typename X> struct baz : foo<X,baz<X> > 
{ 
    X evaluate() { return static_cast<X>("elk"); } 
}; 

template<typename X, typename D> struct foo : D 
{ 
    X operator()() { return static_cast<D*>(this)->evaluate(); } 
}; 

template<typename X, typename D> 
void print_foo(foo<X,D> xyzzx) 
{ 
    cout << "Foo is " << xyzzx() << "\n"; 
} 

int main() 
{ 
    bar<double> br; 
    baz<const char*> bz; 

    print_foo(br); 
    print_foo(bz); 

    return 0; 
} 

編譯器錯誤

foo.C: In instantiation of ‘foo<double, bar<double> >’: 
foo.C:8: instantiated from ‘bar<double>’ 
foo.C:30: instantiated from here 
foo.C:18: error: invalid use of incomplete type ‘struct bar<double>’ 
foo.C:8: error: declaration of ‘struct bar<double>’ 
foo.C: In instantiation of ‘foo<const char*, baz<const char*> >’: 
foo.C:13: instantiated from ‘baz<const char*>’ 
foo.C:31: instantiated from here 
foo.C:18: error: invalid use of incomplete type ‘struct baz<const char*>’ 
foo.C:13: error: declaration of ‘struct baz<const char*>’ 

回答

1

CRTP的想法是有什麼類型及其衍生物是知道一個基類 - 不要讓基類派生自其衍生物。
否則你有以下情況:

  • DerivedBase<Derived>派生,它
  • Derived派生,它
  • Base<Derived>派生,它
  • ...

改爲使用以下內容:

template<typename X, typename D> struct foo // : D 
// ...          ^remove that 
+0

謝謝,我不知道爲什麼我沒有早點收到。 – Butterwaffle 2010-05-30 21:42:32

+2

@Butterwaffle,那是因爲:D - 笑得很滑稽,你不想刪除它:D – Max 2010-05-30 21:45:01