Generic container where type is known only at runtime

T

Terence Wilson

I'm having trouble designing a container for all the basic types(char,
int, float, double). The container should be able to hold contiguous
arrays of type T. I would like some kind of generic iterator for
accessing the data that is type independent in order that I could
write generic algorithms.

Basically I need something like std::vector that does not require a
compile type.

I could use some kind of Variant for each element, but the overhead
would be huge since all the elements have the same type.
 
J

Juha Nieminen

Terence said:
I'm having trouble designing a container for all the basic types(char,
int, float, double). The container should be able to hold contiguous
arrays of type T. I would like some kind of generic iterator for
accessing the data that is type independent in order that I could
write generic algorithms.

Basically I need something like std::vector that does not require a
compile type.

Exactly how do you determine at runtime (instead of at compile time)
the type you need the vector to be of?
 
?

=?iso-8859-1?q?Erik_Wikstr=F6m?=

I'm having trouble designing a container for all the basic types(char,
int, float, double). The container should be able to hold contiguous
arrays of type T. I would like some kind of generic iterator for
accessing the data that is type independent in order that I could
write generic algorithms.

Basically I need something like std::vector that does not require a
compile type.

I could use some kind of Variant for each element, but the overhead
would be huge since all the elements have the same type.

I'm not sure what exactly you are trying to achieve, but I think you
need to create wrappers for the basic types that all derive from a
common baseclass and store pointers to the baseclass in a container.
 
G

Gianni Mariani

I'm having trouble designing a container for all the basic types(char,
int, float, double). The container should be able to hold contiguous
arrays of type T. I would like some kind of generic iterator for
accessing the data that is type independent in order that I could
write generic algorithms.

Basically I need something like std::vector that does not require a
compile type.

I could use some kind of Variant for each element, but the overhead
would be huge since all the elements have the same type.

This should be quite easy - create a template wrapper around a
std::vector. It's like a boosy any (or Austria C++ Any class for a
vector).

What kinds of things do you envisage doing with it ?
 
T

Thomas J. Gritzan

Terence said:
I'm having trouble designing a container for all the basic types(char,
int, float, double). The container should be able to hold contiguous
arrays of type T. I would like some kind of generic iterator for
accessing the data that is type independent in order that I could
write generic algorithms.

Basically I need something like std::vector that does not require a
compile type.

I could use some kind of Variant for each element, but the overhead
would be huge since all the elements have the same type.

What about:

boost::variant< std::vector<char>, std::vector<int>, std::vector<double> >?

So you don't have a variant for the elements but for the vector.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top