0
template<typename...Args> 
class something {
     //...
};

class storage {

  template<typename...Args>
  void build_something_class_obj() {
     //...
  }
private:
  std::vector<???> m_container;
};

How can I store classes like that? Args may vary so I can't make storage template class

  • `something` is a template not a type. What type do you want to store in the vector? – 463035818_is_not_an_ai Jun 07 '20 at 11:29
  • `build_something_class_obj` should create an instance of `something` and push that into the vector? Can you give a bit more context? You need some sort of type erasure when you want to have objects of different type in the same vector – 463035818_is_not_an_ai Jun 07 '20 at 11:36

1 Answers1

1

something is a template not a type, but you can only have objects of one type in a vector. Different instantiations of something are completely unrelated, unless you use a base class:

struct something_base {};

template<typename...Args> 
class something : something_base {
     //...
};

And now you can have a std::vector<std::unique_ptr<something_base>> and push instances of different isntantiations of something into that vector.

There are alternatives for type erasure (eg std::any, std::variant). What is most appropriate depends on what you want to do with the elements in the vector.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
  • I'm using `something` like this: `typedef something*types*/> type` And then creating an object for given type. I'm making a small database and using `class something` to store a table, so I need `storage` to store all the tables – not_so_smart Jun 08 '20 at 08:08
  • @not_so_smart what is `/*types*/` ? Is it a different set of types for each call to `build_something_class_obj` ? Or is it always the same? – 463035818_is_not_an_ai Jun 08 '20 at 08:48
  • It is different set of types, describes what types the table has. `build_something_class_obj` should make an object and push it to vector. – not_so_smart Jun 08 '20 at 11:15
  • @not_so_smart then I do not understand your comment. My answer tries to explain how you can have `something` with different `Args...` in a vector – 463035818_is_not_an_ai Jun 08 '20 at 11:18
  • In `build_something_class_obj()` I want create a new type( `typedef something my_type` ) and object with type `my_type` , then push object pointer into a vector. Sorry for being unclear tho. – not_so_smart Jun 08 '20 at 14:48
  • @not_so_smart sorry, you keep saying the same thing. You want to create objects of different types and push them into the vector. My answer tries to explain how you can do that. Is it not clear? If so try to ask a question... – 463035818_is_not_an_ai Jun 08 '20 at 14:49
  • It is clear, but how can I call methods from `something` then? – not_so_smart Jun 08 '20 at 15:20
  • @not_so_smart it depends. What methods do you want to call? In your code `something` has no methods. (serisouly, I can only answer based on what you write in the question, and as I wrote in the answer details of how to do it depend on what details you need) – 463035818_is_not_an_ai Jun 08 '20 at 15:22
  • @not_so_smart I didn't tell you to use `std::any`. `std::any` is one possibility and I told you that what to choose depends on what you want to do with the elements in the vector and I don't know what that is – 463035818_is_not_an_ai Jun 09 '20 at 08:51