【问题标题】:This template code will not compile. Any ideas? [closed]此模板代码不会编译。有任何想法吗? [关闭]
【发布时间】:2012-07-20 01:06:17
【问题描述】:

我从 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;
}

【问题讨论】:

  • 无法编译,有时是环境设置造成的。可能在 VS2010 中缺少 3rd 方库。
  • 您有多个问题,如果您将用例缩小一点,您更有可能获得帮助。删除 Foo 或 Bar 并一次专注于一个问题。包括您从编译器获得的错误消息,以便那些无法立即访问的人也可以提供帮助。

标签: c++ templates


【解决方案1】:

抱歉,我为 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. 在类BarInterfaceFooInterface 中为基类enable_crtp&lt;FX&gt; 设置公共: public enable_crtp&lt; FX &gt;
  4. foo()bar() 方法中添加调用self() 方法的范围规范: void xxx() { enable_crtp&lt;FX&gt;::self()-&gt;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;
}

【讨论】:

  • 谢谢!我隔开 int / typename 的东西。
  • @Lex +1 这个答案。在我对 Tavison 原始问题的回答 stackoverflow.com/a/11547473/819272 中,我确实使用了一些 Microsoft 特定功能,例如基类中的依赖名称查找。这现在已在答案中得到解决。感谢您的更正。 (直到现在我才注意到这个线程。)
【解决方案2】:
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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-06-15
    • 2013-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-10
    • 1970-01-01
    相关资源
    最近更新 更多