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

What is type erasure in C++?

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

Repaired link

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 的工作原理,但无法理解它。

下面是一个非常简单的类型擦除示例:

// 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)当然除了可以移动