473,810 Members | 3,142 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1731
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.com 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.

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.com 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.

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<cha r>, std::vector<int >, std::vector<dou ble?

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
1896
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 elements can be themselves containers:
5
5611
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 example, a list). I've coded it in ADA, and its quite easy to do, but there is a thing I cannot decide when doing it in C. This is about the actual data storing in a
17
3335
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 way they are. ***** Summary & Questions ***** In a nutshell, the current .NET generics & .NET framework make it sometimes difficult or even impossible to write truly generic code. For example, it seems to be impossible to write a truly generic
4
2047
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# generic use constraint by type,complier is relaxed, but it is very limited to programmer.Is there a way to get merits of both? Maybe the following way can achieve this purpose : //First add a keyword "constrant" to modify class or struct just...
4
4937
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 the containers in the STL, including iterator usage. Info on the library can be found here.. http://www.visuallandlord.com/developers_corner/open_source/tree_container_library/overview.php The library and sample code can be downloaded from this...
3
2501
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 this in my Multi-Dimensional Array named people Then, I would then like to say: new Person<People.ToString>(); Where ToString() would output a built in datatype or a struct/object that I
7
1858
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 = _next' is a 'field' but is used like a 'type'): BaseWizardForm nextForm = (BaseWizardForm)Activator.CreateInstance<_next>();
3
3834
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 function overload and the virtual functionality. As I understand when you use templates you just increase the runtime performance since the code is resolved at compile time. How come people are mixing templates with generic programming when all...
11
2556
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 generics instead of System.Object. I want the code in Form1_Load to remain exactly the same, but in the background I want to use generics. I'm trying to get a better understanding of how it works and I'm a little stuck.
0
9722
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10644
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10379
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10124
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7664
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6882
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5690
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3863
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3015
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.