I have only recently started working with variadic templates.
Here is my problem:
I have a function called "addTo()
" that adds a new object to a collection. The type of the object is the same as the type of the class the function resides in (its a template class). addTo()
also uses a parameter pack to fill the constructor. addTo
also recieves a key for the map (thats the collection). Now the collection stores the objects using a std::shared_ptr
. Now to improve flexibility I want to add the ability to add a existing object to the collection, which will allow me to also add anything thats assignable.
here is an example (pseudo code)
class A{
//whatever
~~some function that can be overloaded
A(int,float);
}
class B:public A{
//whatever
~~overloads that function
}
//In some other function
AddToClass<int> instance;
instance.addTo(key,12,2.2); //this is how it already works
instance.addTo(key, new B(12,3.5)); //this is how I also want it to work
I dont know if this is achievable or how I can achieve this, however I beg you to explain the (possible) solution in detail since I still have loads of problems understanding parameter packs.
What I have tried
- I tried using dynamic cast but I didnt manage to make it so that the parameter pack gets expanded (because I don't really get how to use them yet other than very basic function forwarding)
- I tried just putting it in the
std::make_shared
call (the constructor) but that doesn't work because I don't want to force the types to have a constructor for each derived class (seems counter intuitive and bad practice)
Please explain to me how to do this (if its possible) in detail. It would really help me out a lot :)
---More info---
Here is an example of how the addToClass
could look like (pseudo code, written from the top of my head):
template<class t>
class addToClass{
private:
std::map<key, shared_ptr<t>> collection;'
//sth else
public:
template<typename... Args>
void addTo(const Key &key, Args...args){
collection.insert(std::pair<key, shared_ptr<t>>(key,std::make_shared<t>(std::forward<Args>(args)...));
}
//other stuff
}
Now everything already works, all I want is that "addTo()
" can also accept pointers of the type t
or pointers to some class that derives from t
. That would allow me to make the function a lot more flexible and save me loads of work in some cases.
Aucun commentaire:
Enregistrer un commentaire