简体   繁体   English

如何创建一个向量来保存同一模板类的所有类型的元素

[英]How to create a vector that saves elements of all types of the same template class

I have a template class: 我有一个模板类:

template<typename A, typename B, typename C>
class blah{...};

I want to create a std::vector<> to save any possible objects of blah. 我想创建一个std :: vector <>来保存任何可能的对象。

for example 例如

blah<float, int, string>

or 要么

blah<string, string, char>

those blah objects, although originated from the same template class, are essentially different types. 这些对象虽然起源于同一模板类,但实际上是不同的类型。 How can I declare a std::vector to save them all? 如何声明一个std :: vector来保存它们?

I thought about using std::variant, but I can't because to use it, I need to enumerate all possible template parameter combinations: 我考虑过使用std :: variant,但是我不能,因为要使用它,我需要枚举所有可能的模板参数组合:

std::variant<all possible template parameter combinations>

I thought about using std::any, I can't because I don't know how to type cast from std::any back to the right type? 我考虑过使用std :: any,因为我不知道如何键入从std :: any返回正确类型的类型?

I thought about using a base class: 我考虑过使用基类:

template<typename A, typename B, typename C>
class blah : public blah_base
{...};

and declare a vector like this: 并声明一个像这样的向量:

std::vector<blah_base*>

but again, I still don't know how to type cast from blah_base* back to the right type? 但是同样,我仍然不知道如何将blah_base *的类型转换回正确的类型?

Basically, what you need is polymorphism. 基本上,您需要的是多态性。 All of the options you were exploring are polymorphism, and that is the best way for this to be solved. 您正在探索的所有选项都是多态的,这是解决此问题的最佳方法。 However, you say this and other variations: 但是,您说这和其他变体:

I don't know how to type cast from blah_base* back to the right type 我不知道如何将blah_base *的类型转换回正确的类型

If you find yourself needing to do this, you are doing something wrong. 如果您发现自己需要这样做,那说明您做错了。 Either it doesn't make sense to have a std::vector<blah<...>> , or your blahs all need to meet the blah_base contract. 要么拥有std::vector<blah<...>>没有任何意义,要么所有的std::vector<blah<...>>都需要满足blah_base合同。

You could solve this with a virtual function: 您可以使用虚函数解决此问题:

class blah_base {
public:
    virtual void foo() = 0;
};

Then, rather than having to tell which type of blah you had, you'd just call the appropriate foo function that does what you want. 然后,您不必调用哪种类型的blah ,而只需调用执行所需功能的适当的foo函数即可。

Well, without knowing too much about what your use case is I will assume you will have three vectors; 好吧,在不完全了解您的用例的情况下,我将假定您将拥有三个向量。 one for each template type. 每个模板类型一个。 You could implement blah as such: 您可以这样实现blah

#include <vector>

template<typename A, typename B, typename C>
class blah
{
public:
    // Put stuff here.
    void AddToVectorA(A item);
    void AddToVectorB(B item);
    void AddToVectorC(C item);
private:
    std::vector<A> _a;
    std::vector<B> _b;
    std::vector<C> _c;
};

template<typename A, typename B, typename C>
void blah<A,B,C>::AddToVectorA(A item)
{
    _a.push_back(item);
}

template<typename A, typename B, typename C>
void blah<A,B,C>::AddToVectorB(B item)
{
    _b.push_back(item);
}

template<typename A, typename B, typename C>
void blah<A,B,C>::AddToVectorC(C item)
{
    _c.push_back(item);
}

int main()
{
    blah<int, double, float> b;
    b.AddToVectorA(2);

    return 0;
}

You could also make the three vectors public and people can use them however; 您也可以将这三个向量公开,但是人们可以使用它们。 you could also look into tuple . 您也可以查看tuple FYI: I now tested the code, it works. 仅供参考:我现在测试了代码,它可以工作。

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

相关问题 如何为基类泛型类型创建向量以使用具体类型元素 - How to create a vector for base class generic types to use concrete type elements C++ 模板 Class 和 Static 成员 - 所有类型的 class 相同 - C++ Template Class with Static Members - Same for all types of the class 模板类中的静态成员。 所有类型的类的相同计数器 - Static member in a template class. Same counter for all types of the class 如何查看向量中的所有元素是否相同? - how to see if all elements in vector are the same? 将向量的元素与相同向量的所有元素相乘 - Multiply elements of a vector with all elements of same vector 如何在具有依赖元素类型的模板类构造函数中初始化矢量 - How to initialize vector in template class constructor with dependent element types 如何使用相同的模板语法创建一个function_list &lt;&gt;类来保存std :: function &lt;&gt; s的向量? - How do I create a function_list<> class to a hold a vector of std::function<>s with the same template syntax? 使用不同/未知类型的类模板初始化向量 - Initializing a vector with class template of different / unknown types 验证多态向量元素的具体类型是相同的 - Veryfing that concrete types of polymorphic vector elements are the same 如何为所有派生类型部分专门化一个类模板? - How to partially specialize a class template for all derived types?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM