C++ 什么是C++;?

C++ 什么是C++;?,c++,type-erasure,C++,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_anima

所以我在看书。但该文章中的代码似乎部分不正确,例如:

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(); }
};
这些错误使我不敢再读这篇文章了

无论如何;谁能教C++中什么类型的擦除,用简单的例子?< /P>
我想了解它以了解
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() };
}
//键入内容的擦除部分
类类型删除文件夹
{
结构类型keeperbase
{
虚拟~TypeKeeperBase(){}
};
模板
结构TypeKeeper:TypeKeeperBase
{
删除类型存储对象;
TypeKeeper(橡皮擦类型和对象):storedObject(std::move(对象)){}
};
std::持有的唯一ptr;
公众:
模板
TypeReasedHolder(ReasedType objectToStore):保持(新的TypeKeeper(std::move(objectToStore)))
{}
};
//客户端代码方面的事情
结构A
{

~A()
,并将
新的AnimalWrapper
替换为
WrapAnimal
@NathanOliver在这个问题上,OP已经知道类型擦除的基本概念。我看不出错误。模板参数不是简单地推导出来的吗?@AdrianMcCarthy先生,类模板不是这样的,构造函数不会为CLA推导模板参数s templates AFAIK.Hm,为什么不使用
&&
(通用参考(?)作为
TypeErasedHolder
std::forward
的构造函数参数将其传递给
TypeKeeper
?(试图理解这里,你似乎总是拿着一个副本,然后从副本移动,而不是移动原始对象)@JonasWielicki保持“非常简单”示例方面。使用转发引用还需要使用
std::remove_reference
作为
TypeKeeper
等的模板参数。感谢您的澄清!@AngelusMortis没有转发引用(以前称为“通用引用”)在现在的代码中,只是一个普通的旧右值引用。但是如果您愿意,我也可以删除它(或者干脆不移动,而使用普通的旧
常量和副本)。(2)当然,除了可销毁之外。另外,可移动的需求很容易修复:添加基于模板的构造。
模板结构模板模板模板{};template emplace_as_t emplace_as={};
然后添加ctor
模板类型擦除文件夹(emplace_as_t,Args&…Args)
等。但这不是基本类型擦除。
// 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() };
}