-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 の間違ったテンプレート クラス宣言 -- に置き換えtypenameますinttemplate<template<int> class F, int X> class BarInterface

  2. public をメソッドに設定しfoo()ますbar()public: void xxx() { self()->do_xxx(); }

  3. enable_crtp<FX>クラスBarInterfaceおよびの基本クラスに public を設定しFooInterfaceます。 public 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(); }    
};

次に、プライベートメンバー関数へのアクセスに関する2つのエラーを修正する必要があります

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 に答える