2012-07-20 57 views
-2

我從rhalbersma得到了這段代碼,但它不能在VC 2010中編譯。我不知道我在做什麼錯。此模板代碼不會編譯。有任何想法嗎?

template<typename Derived> 
struct enable_crtp 
{ 
private: 
    // typedefs 

    typedef enable_crtp Base; 

public: 
    // casting "down" the inheritance hierarchy 
    Derived const* self() const 
    {     
     return static_cast<Derived const*>(this); 
    } 

    // write the non-const version in terms of the const version 
    // Effective C++ 3rd ed., Item 3 (p. 24-25) 
    Derived* self() 
    { 
     return const_cast<Derived*>(static_cast<Base const*>(this)->self()); 
    }  

protected: 
    // disable deletion of Derived* through Base* 
    // enable deletion of Base* through Derived* 
    ~enable_crtp() 
    { 
     // no-op 
    } 
}; 

template<typename FX> 
class FooInterface 
    : 
    private enable_crtp<FX> 
{ 
public: 
    // interface 
    void foo() { self()->do_foo(); } 
}; 

class FooImpl 
    : 
    public FooInterface<FooImpl> 
{ 
private: 
    // implementation 
    friend class FooInterface<FooImpl> ; 
    void do_foo() { std::cout << "Foo\n"; } 
}; 

class AnotherFooImpl 
    : 
    public FooInterface<AnotherFooImpl> 
{ 
private: 
    // implementation 
    friend class FooInterface<AnotherFooImpl>; 
    void do_foo() { std::cout << "AnotherFoo\n"; } 
}; 

template<template<typename> class F, int X> 
class BarInterface 
    : 
    private enable_crtp< F<X> > 
{ 
// interface 
void bar() { self()->do_bar(); }  
}; 

template< int X > 
class BarImpl 
    : 
    public BarInterface< BarImpl, X > 
{ 
private: 
    // implementation 
    friend class BarInterface< ::BarImpl, X >; 
    void do_bar() { std::cout << X << "\n"; }  
}; 

int main() 
{ 
    FooImpl f1;   
    AnotherFooImpl f2; 

    BarImpl<1> b1; 
    BarImpl<2> b2; 

    f1.foo(); 
    f2.foo(); 
    b1.bar(); 
    b2.bar(); 

    return 0; 
} 
+5

什麼是編譯錯誤? – jrad 2012-07-20 01:10:03

+0

無法編譯,有時是由環境設置引起的。也許在VS2010中缺少第三方庫。 – 2012-07-20 01:17:30

+0

如果您縮小用例範圍,您有多個問題並且更可能獲得幫助。刪除Foo或Bar,然後集中解決一個問題。包含從編譯器獲得的錯誤消息,以便那些不能立即訪問的錯誤消息也可以提供幫助。 – 2012-07-20 01:59:33

回答

1

對不起的兩個錯誤,但我重拍了GCC-4.4和可以將此代碼不檢查VC 2010. 錯誤:

  1. 錯誤模板類聲明模板BarInterface - 取代typenameinttemplate<template<int> class F, int X> class BarInterface

  2. 設置公衆foo()bar()方法:在類BarInterface爲基類enable_crtp<FX> public: void xxx() { self()->do_xxx(); }

  3. 設置公共和FooInterfacepublic enable_crtp<FX>
  4. 添加範圍規範調用self()方法foo()bar()方法: void xxx() { enable_crtp<FX>::self()->do_xxx(); }

最後我獲得工作代碼:

template<typename Derived> 
struct enable_crtp 
{ 
private: 
// typedefs 

    typedef enable_crtp Base; 

public: 
    // casting "down" the inheritance hierarchy 
    Derived const* self() const 
    {     
     return static_cast<Derived const*>(this); 
    } 

    // write the non-const version in terms of the const version 
    // Effective C++ 3rd ed., Item 3 (p. 24-25) 
    Derived* self() 
    { 
     return const_cast<Derived*>(static_cast<Base const*>(this)->self()); 
    }  

protected: 
    // disable deletion of Derived* through Base* 
    // enable deletion of Base* through Derived* 
    ~enable_crtp() 
    { 
     // no-op 
    } 
}; 

template<typename FX> 
class FooInterface 
    : 
    public enable_crtp<FX> 
{ 
public: 
    // interface 
    void foo() { enable_crtp<FX>::self()->do_foo(); } 
}; 

class FooImpl 
    : 
    public FooInterface<FooImpl> 
{ 
private: 
    // implementation 
    friend class FooInterface<FooImpl> ; 
    void do_foo() { std::cout << "Foo\n"; } 
}; 

class AnotherFooImpl 
    : 
    public FooInterface<AnotherFooImpl> 
{ 
private: 
    // implementation 
    friend class FooInterface<AnotherFooImpl>; 
    void do_foo() { std::cout << "AnotherFoo\n"; } 
}; 

template<template<int> class F, int X> 
class BarInterface 
    : 
    public enable_crtp< F<X> > 
{ 
public: 
// interface 
void bar() { enable_crtp< F<X> >::self()->do_bar(); }  
}; 

template< int X > 
class BarImpl 
    : 
    public BarInterface< BarImpl, X > 
{ 
private: 
    // implementation 
    friend class BarInterface< ::BarImpl, X >; 
    void do_bar() const { std::cout << X << "\n"; }  
}; 

int main() 
{ 
    FooImpl f1;   
    AnotherFooImpl f2; 

    BarImpl<1> b1; 
    BarImpl<2> b2; 

    f1.foo(); 
    f2.foo(); 
    b1.bar(); 
    b2.bar(); 

    return 0; 
} 
+0

謝謝!我分開int/typename的東西。 – Tavison 2012-07-20 16:18:00

+0

@Lex +1這個答案。在我對Tavison的原始問題的回答http://stackoverflow.com/a/11547473/819272中,我確實使用了一些Microsoft特定的功能,例如在基類中進行從屬名稱查找。現在這個問題已經解決了。感謝您的更正。 (我直到現在才注意到這個線程。) – TemplateRex 2012-11-27 19:41:33

1
template<template<typename> class F, int X> 
class BarInterface 
: 
private enable_crtp< F<X> > 
{ 
// interface 
void bar() { self()->do_bar(); }  
}; 

我猜

template<template<int > class F, int X> 
class BarInterface 
: 
private enable_crtp< F<X> > 
{ 
// interface 
void bar() { self()->do_bar(); }  
}; 

那麼你需要解決有關訪問私有成員函數

template<typename Derived> 
struct enable_crtp 
{ 
private: 
    // typedefs 

    typedef enable_crtp Base; 

public: 
    // casting "down" the inheritance hierarchy 
    Derived const* self() const 
    {     
     return static_cast<Derived const*>(this); 
    } 

    // write the non-const version in terms of the const version 
    // Effective C++ 3rd ed., Item 3 (p. 24-25) 
    Derived* self() 
    { 
     return const_cast<Derived*>(static_cast<Base const*>(this)->self()); 
    }  

protected: 
    // disable deletion of Derived* through Base* 
    // enable deletion of Base* through Derived* 
    ~enable_crtp() 
    { 
     // no-op 
    } 
}; 

template<typename FX> 
class FooInterface 
    : 
    private enable_crtp<FX> 
{ 
public: 
    // interface 
    void foo() { self()->do_foo(); } 
}; 

class FooImpl 
    : 
    public FooInterface<FooImpl> 
{ 
private: 
    // implementation 
    friend class FooInterface<FooImpl> ; 
    void do_foo() { std::cout << "Foo\n"; } 
}; 

class AnotherFooImpl 
    : 
    public FooInterface<AnotherFooImpl> 
{ 
private: 
    // implementation 
    friend class FooInterface<AnotherFooImpl>; 
    void do_foo() { std::cout << "AnotherFoo\n"; } 
}; 

template<template<int > class F, int X> 
class BarInterface 
    : 
    private enable_crtp< F<X> > 
{ 
    // interface 
public: void bar() { self()->do_bar(); }  
}; 

template< int X > 
class BarImpl 
    : 
    public BarInterface< BarImpl, X > 
{ 
private: 
    // implementation 
    friend class BarInterface< ::BarImpl, X >; 
    void do_bar() { std::cout << X << "\n"; }  
}; 

int main() 
{ 
    FooImpl f1;   
    AnotherFooImpl f2; 

    BarImpl<1> b1; 
    BarImpl<2> b2; 

    f1.foo(); 
    f2.foo(); 
    b1.bar(); 
    b2.bar(); 

    return 0; 
}