什么是C ++中的类型擦除?

时间:2016-01-15 16:24:15

标签: c++ type-erasure

所以我正在阅读this article about type erasure。但该文章中的代码似乎部分不正确,例如:

template <typename T>
class AnimalWrapper : public MyAnimal
{
    const T &m_animal;

public:
    AnimalWrapper(const T &animal)
        : m_animal(animal)
    { }

    const char *see() const { return m_animal.see(); }
    const char *say() const { return m_animal.say(); }
};

接着是

void pullTheString()
{
    MyAnimal *animals[] = 
    {
        new AnimalWrapper(Cow()), /* oO , isn't template argument missing? */
        ....
    };
}

这些错误使我不鼓励在文章中进一步阅读。

反正;任何人都可以用简单的例子教C ++中的类型擦除吗?

我想了解它以了解std::function如何运作,但无法理解它。

1 个答案:

答案 0 :(得分:10)

这是一个非常简单的类型擦除实例:

// Type erasure side of things

class TypeErasedHolder
{
  struct TypeKeeperBase
  {
    virtual ~TypeKeeperBase() {}
  };

  template <class ErasedType>
  struct TypeKeeper : TypeKeeperBase
  {
    ErasedType storedObject;

    TypeKeeper(ErasedType&& object) : storedObject(std::move(object)) {}
  };

  std::unique_ptr<TypeKeeperBase> held;

public:
  template <class ErasedType>
  TypeErasedHolder(ErasedType objectToStore) : held(new TypeKeeper<ErasedType>(std::move(objectToStore)))
  {}
};

// Client code side of things

struct A
{
  ~A() { std::cout << "Destroyed an A\n"; }
};

struct B
{
  ~B() { std::cout << "Destroyed a B\n"; }
};

int main()
{
  TypeErasedHolder holders[] = { A(), A(), B(), A() };
}

[Live example]

如您所见,TypeErasedHolder可以存储任意类型的对象,并正确销毁它们。重要的是,它不对受支持的类型(1)施加任何限制:例如,它们不必从公共基础派生。

(1)当然,除了可移动之外。