473,378 Members | 1,209 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Generic container where type is known only at runtime

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.

May 11 '07 #1
4 1711
Terence Wilson wrote:
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?
May 11 '07 #2
On 11 Maj, 06:45, Terence Wilson <t...@latte.comwrote:
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.

--
Erik Wikström

May 11 '07 #3
On May 11, 2:45 pm, Terence Wilson <t...@latte.comwrote:
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 ?

May 11 '07 #4
Terence Wilson schrieb:
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.

--
Thomas
http://www.netmeister.org/news/learn2quote.html
May 11 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Luc Claustres | last post by:
I have a generic container such as: template<class T> class Container { // some data structure that store elements of type T } I use this container in a hierarchical manner, that is...
5
by: Alberto Giménez | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hello, I have a simple question, and after a *very long* google search I still can't get it. It's about generic abstract data types (for...
17
by: Andreas Huber | last post by:
What follows is a discussion of my experience with .NET generics & the ..NET framework (as implemented in the Visual Studio 2005 Beta 1), which leads to questions as to why certain things are the...
4
by: Jethro Guo | last post by:
C++ template use constraint by signature,It's very flexible to programmer but complex for complier, and at most time programmer can not get clear error message from complier if error occur. C#...
4
by: Mitchel Haas | last post by:
Hello, Feeling a need for a generic tree container to supplement the available containers in the STL, I've created a generic tree container library (TCL). The library usage is very much like...
3
by: Scottie_do | last post by:
I'm considering switching to C# and using VS2005, but I'd like to know if I can have a list of values at runtime and then specify (at runtime) what it's value type is. For example, I would have...
7
by: Brad Wood | last post by:
The following works fine; passing a type to CreateInstance: private Type _next; BaseWizardForm nextForm = (BaseWizardForm)Activator.CreateInstance( _next ); So why doesn't this work (error =...
3
by: Johs | last post by:
I have read that when you are using templates you are making generic programs. But I don't see whats so generic about templates. You can make generic programs without templates through the use of...
11
by: Scott Stark | last post by:
Hello, The code below represents a singly-linked list that accepts any type of object. You can see I'm represting the Data variable a System.Object. How would I update this code to use...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.