衍生出奇怪的重复模板和协方差

时间:2013-06-19 21:01:51

标签: c++ visual-studio-2010 templates crtp f-bounded-polymorphism

假设我有一个克隆派生类的基类:

class Base
{
    public:
        virtual Base * clone()
        {
            return new Base();
        }

        // ...
};

我有一组派生类,使用奇怪的重复模板模式实现:

template <class T>
class CRTP : public Base
{
    public:
        virtual T * clone()
        {
            return new T();
        }

        // ...
};

我试图从中得到更多:

class Derived : public CRTP<Derived>
{
    public:
        // ...
};

我得到了编译错误:

error C2555: 'CRTP<T>::clone': overriding virtual function return type differs and is not covariant from 'Base::clone'

我意识到这可能是编译器在实例化CRTP时不完全知道Derived的继承树的结果。此外,用(Base *)替换返回类型(T *)也会编译。但是,我想知道是否有一项工作可以保留上述语义。

2 个答案:

答案 0 :(得分:3)

一个不那么漂亮的解决方法。

class Base
{
    protected:
        virtual Base * clone_p()
        {
            return new Base();
        }
};


template <class T>
class CRTP : public Base
{
    protected:
        virtual CRTP* clone_p()
        {
            return new T;
        }
    public:
        T* clone()
        {
            CRTP* res = clone_p();
            return static_cast<T*>(res);
        }
};


class Derived : public CRTP<Derived>
{
    public:
};

如果您觉得它更安全,请使用dynamic_cast<>代替static

答案 1 :(得分:1)

如果您不得不使用不同的语法来指定完整类型,则可以执行以下操作(警告:未经测试的代码):

让我们先从机器开始:

// this gives the complete type which needs to be used to create objects
// and provides the implementation of clone()
template<typename T> class Cloneable:
  public T
{
public:
  template<typename... U> Cloneable(U&&... u): T(std::forward<U>(u) ...) {}
  T* clone() { return new Cloneable(*this); }
private:
  // this makes the class complete
  // Note: T:: to make it type dependent, so it can be found despite not yet defined
  typename T::CloneableBase::CloneableKey unlock() {}
};

// this provides the clone function prototype and also makes sure that only
// Cloneable<T> can be instantiated
class CloneableBase
{
  template<typename T> friend class Cloneable;

  // this type is only accessible to Clonerable instances
  struct CloneableKey {};

  // this has to be implemented to complete the class; only Cloneable instances can do that
  virtual CloneableKey unlock() = 0;
public:
  virtual CloneableBase* clone() = 0;
  virtual ~CloneableBase() {}
};

好的,现在是实际的类层次结构。那个很标准;没有CRTP中间体或其他并发症。但是没有类实现clone函数,但所有类都从CloneableBase继承(直接或间接)声明。

// Base inherits clone() from CloneableBase
class Base:
  public CloneableBase
{
  // ...
};

// Derived can inherit normally from Base, nothing special here
class Derived:
  public Base
{
  // ...
};

以下是创建对象的方法:

// However, to create new instances, we actually need to use Cloneable<Derived>
Cloneable<Derived> someObject;
Derived* ptr = new Cloneable<Derived>(whatever);

// Now we clone the objects
Derived* clone1 = someObject.clone();
Derived* clone2 = ptr->clone();

// we can get rid og the objects the usual way:
delete ptr;
delete clone1;
delete clone2;

请注意,Cloneable<Derived>是一个Derived(它是一个子类),因此您只需要使用Cloneable进行构造,否则可以假装使用{{1对象(嗯,Derived也会将其标识为tyepinfo)。