静态多态和模板容器

static polymorphism and template Containers

我正在尝试使用静态多态性和模板来创建一个可以容纳更多一种类型的容器,据我所知,模板无法完成,但我希望我能错了,有办法。 我有以下 类:

template<class Derived>
class base
{
public:
    base(string);
    void clean()
    {
        cout << "I'm cleannig \n";
    }
    void process()
    {
        static_cast<Derived*>(this)->setup();
        static_cast<Derived*>(this)->run();
        static_cast<Derived*>(this)->cleanup();
    }
    string name;
};

template<class Derived>
base<Derived>::base(string y):name(y)
{
}


class derived : public  base<derived> 
{
    friend class base<derived>;
    void setup() {cout << "derived setup \n"; }
    void run() { cout << "derived run \n"; }
    void cleanup() { cout << "derived cleanup \n"; }

};


class derived1 : public base<derived1> 
{
    friend class base<derived1>;
    void setup() {cout << "derived1 setup \n"; }
    void run() { cout << "derived1 run \n"; }
    void cleanup() { cout << "derived1 cleanup \n"; }
};

我不想创建一个可以容纳它们的容器,我试过这段代码 -

template <class T>
class Y{
 public:
 std::vector<base<T>> m_vec;   

};


template <typename T>
class D:public Y<T>
{
    public:
    friend class Y<T>;
    void print()
    {
        for(auto& e: Y<T>::m_vec)
        {
            e.process();
        }
    } 
};


int main()
{ 
    base<derived>* b =  new base<derived>;
    base<derived1>* c =  new base<derived1>;

    D<derived> y;
    y.m_vec.push_back(b);
    y.m_vec.push_back(c);
    y.print();
}

但它不起作用 我试着这样做:

 y.m_vec.push_back(static_cast<base<derived>>(c));

我收到了这个错误:

error: no matching function for call to ‘std::vector, std::allocator > >::push_back(base*&)’ y.m_vec.push_back(b);

经过一些测试和挖掘,答案是没有办法做到这一点。 但是你可以像@formerlyknownas_463035818建议的那样使用std::any 将 std::vector 声明为 :

`std::vector<std::any> m_vec;`

而不是

std::vector<base<T>> m_vec;

并使用 boost demangle 函数获取类型 -

std::string name(boost::core::demangle(e.type().name()));

然后使用某种工厂函数 any_cast 到你需要的类型

 if(!name.compare("base<derived1>*") )
 {
     try {
             auto* r = any_cast<base<derived1>*>(e);
             r->process();
         }
         catch(const std::bad_any_cast& e) {
             std::cout << e.what() << '\n';
         }
  }
  else
  {
     try {
         auto *r = any_cast<base<derived> *>(e);
         r->process();
     }
     catch(const std::bad_any_cast& e) {
         std::cout << e.what() << '\n';
     }
   }

或者,您可以使用 class any 的 type() 函数,而不是使用 demangle 名称并使用字符串比较,然后像这样比较 typeid:

    if(e.type()==typeid(base<derived1>*))