greenoldman
greenoldman

Reputation: 21062

How to store various types as one type in C++?

My C++ is a bit rusty nowadays so stupid question -- how to store various types as one type?

What I have is type info (not in C++ sense) and data itself. The first one is just an enum, data can be int, or vector<string>. I have to store them in one vector<T> -- and the question is what should be T? void*?

Later based on my enum type I can down- or up-cast the data, load it, save it, pass it, no problem here.

In C# it would be object, but C++ does not have common root type, and also my types do not have common root type.

I use C++11.

Upvotes: 1

Views: 66

Answers (2)

Nevin
Nevin

Reputation: 4853

If you have a bounded list of known types (it looks like you do if the only data types are int and vector<string>, a good bet would be to use a Boost Variant.


Upvotes: 1

Some programmer dude
Some programmer dude

Reputation: 409146

You could probably do it with Boost Any.

Upvotes: 1

Related Questions