-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;
}
4

2 回答 2

2

抱歉,我为 GCC-4.4 重新制作了这段代码,无法检查 VC 2010。错误:

  1. 模板 BarInterface 中的模板类声明错误 - 替换typenameinttemplate<template<int> class F, int X> class BarInterface

  2. 将 public 设置为foo()bar()方法: public: void xxx() { self()->do_xxx(); }

  3. enable_crtp<FX>为类中的基类设置 publicBarInterfaceFooInterfacepublic 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;
}
于 2012-07-20T01:54:15.503 回答
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;
}
于 2012-07-20T01:52:03.570 回答