2012-07-20 58 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; 
}