简体   繁体   English

矢量模板问题

[英]Vector template question

Question as I have not done much with vectors and templates. 问题是我对向量和模板没有做太多事情。

If I have a class foo that is templated class and I want to create a vector of foo pointers regardless of foo type, what would the syntax look like? 如果我有一个作为模板化类的foo类,并且想创建一个foo指针的向量,而不管foo的类型如何,那么语法会是什么样?

There is no direct way to do this. 没有直接的方法可以做到这一点。 Different instantiations of the same template are treated as distinct classes with no relation to one another. 同一模板的不同实例被视为彼此无关的不同类。

If you want to treat them uniformly, one option is to create a base class that the template foo class then inherits from. 如果要统一对待它们,一个选择是创建一个基类,然后从该基类中继承foo类。 For example: 例如:

class foo_base {
    /* ... */
};

template <typename T> class foo: public foo_base {
    /* ... */
};

Now, you can create a vector<foo_base*> to store pointers to foo_base objects, which are in turn all specializations of foo . 现在,您可以创建一个vector<foo_base*>来存储指向foo_base对象的指针, foo_base对象又是foo所有特化。

You wouldn't. 你不会的 Every instantiation of the class template foo (eg foo<int> , foo<char> etc) is a distinct type . 类模板foo每个实例化(例如foo<int>foo<char>等)都是不同的类型 foo itself is not a type . foo本身不是类型

You'd need some base class for them and store pointers, making use of polymorphism. 您需要为它们提供一些基类并利用多态来存储指针。

You cannot have a vector of foo<?> . 您不能有foo<?>的向量。 The compiler when it creates the vector needs to know precisely what type it stores because every instantiation of foo<?> could potentially have a different size and entirely different members (because you can provide explicit and partial specializations). 编译器在创建向量时需要准确知道其存储的类型,因为foo<?>每个实例都可能具有不同的大小和完全不同的成员(因为您可以提供显式和部分专业化的知识)。

You need to give the foo<T> a common base class and then put baseclass pointers into the vector (preferably using shared_ptr or something similar). 您需要给foo<T>一个通用的基类,然后将baseclass指针放到向量中(最好使用shared_ptr或类似的东西)。

As an alternative there are ready classes that hide this from you and act like a container of references. 作为替代方案,可以使用现成的类将其隐藏起来,并像引用容器一样工作。 Among them is boost::ptr_vector , which however also need a common type to store. 其中包括boost::ptr_vector ,但是还需要一个通用类型来存储。

class common_base { 
  /* functions to access the non-templates parts of a foo<T> .. */ 
};

template<typename T> class foo : public common_base { };

Not possible. 不可能。 When you use a class template anywhere, you need it instantiated on a type. 在任何地方使用类模板时,都需要在类型上实例化它。
One possibility to circumvent that, is to provide a polymorphic interface base class for foo and have a vector of those pointers. 一种可能的解决方法是,为foo提供一个多态接口基类,并为这些指针提供向量。

class IFoo{
  virtual void bar() = 0;
  virtual int baz() = 0;
};

template<class T>
class Foo : IFoo{
   // concrete implementations for bar and baz
};

// somewhere in your code:
std::vector<IFoo*> vecFoo.
vecFoo.push_back(new Foo<int>);

The obvious problem with that is, that you can't need to know each possible return value for your bar and baz functions. 显而易见的问题是,您不需要知道barbaz函数的每个可能的返回值。

If you are trying to emulate concept: "vector of pointers to Foo" without specifying T, you are trying to emulate template typedefs that are not supported by current standard. 如果您尝试模拟概念:“ Foo的指针向量”而不指定T,则尝试模拟当前标准不支持的模板typedef。

The workaround is 解决方法是

template <class T>
my_vector
{
   typedef std::vector<Foo<T>*> type;
}

If, instead, you can afford polymorphic interface for your Foo guy, I would do that as it was already suggested. 相反,如果您可以为Foo家伙买得起多态接口,那么我会按照建议的那样进行操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM