How would you implement a list composed of different types in C++, similar to Python's lists?
For example:
Code:a = [3.14, "banana", 12, ["another list", 42]]
How would you implement a list composed of different types in C++, similar to Python's lists?
For example:
Code:a = [3.14, "banana", 12, ["another list", 42]]
Boost supports some stuff similar to that. Are you just asking for the sake of posterity or do you have a legitimate reason for such a thing?
If all entry's will have the same types then I'd create a struct containing all the types then use std::list or another std class template. the template you need to use really depends on what your going to do with your list.
Try to help all less knowledgeable than yourself, within
the limits provided by time, complexity and tolerance.
- Nor
I believe boost::variant is for when you have a specific set of types you'll be using, and boost::any is for when the set of types is open-ended.
Without knowing any already made library, I would use a I list of void* and a list of their respective types. And a functions/marcos for the dereferencing, all pact up in a nice class. But whatever you do you won't be able to manipulate them as nicely as in the built-in lists in Python. So it would be kind of worthless