Search code examples
c++listpush-back

std::list push_back unrelated types


Is it possible to push_back to a list of userClass a variable with different type?

list<MyClass*>* myList
list<int>* NewData

// -- ..some population of NewDat.. --

myList->push_back(NewData);

Solution

  • No. What you are asking for is not possible. myList stores MyClass*s, not ints or lists of ints. If you want a generic list, you can use std::list<std::any>, or, in case you know the types in advance, std::list<std::variant<(types here)>>.