473,406 Members | 2,867 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,406 software developers and data experts.

containers of templates

Hi,
I want to create a vector of templates, but I've come across a problem....
Here is a trimmed version of my template definition:
template<class T>
class variant
{
public:

variant(T val)
{
_val = val;
}
private:
T _val;
};
i want to create a vector of these, where T can be various types,
however when i create the vector, i need to specify a single type, eg:

std::vector<variant<XXX>> vars;

where XXX is a specific type.

Someone recommended i make the template extend a superclass, but im not
sure how exactly that would work.
Does that make sense?
Is there a way round this?
Thanks

Andrew Bullock
Jul 23 '05 #1
2 1240
Andrew Bullock wrote:
I want to create a vector of templates, but I've come across a
problem....
A container of templates is itself a template. If you need to have
an object of that type, you'd have to _specialise_ it (explicitly or
implicitly).
Here is a trimmed version of my template definition:
template<class T>
class variant
{
public:

variant(T val)
{
_val = val;
}
private:
T _val;
};
i want to create a vector of these, where T can be various types,
however when i create the vector, i need to specify a single type, eg:

std::vector<variant<XXX>> vars;

where XXX is a specific type.
Yep.
Someone recommended i make the template extend a superclass, but im
not sure how exactly that would work.
If you have a common base _class_ of all 'variant<>', you could store
pointers to those in your vector, and treat them polymorphically...
Does that make sense?
Yes, but...
Is there a way round this?


Nope.

You might want to look at "heterogenous container" on the Web.

V
Jul 23 '05 #2
Andrew Bullock wrote:
Hi,
I want to create a vector of templates, but I've come across a problem....


Victor already addressed your questions, but note that there's a
perfectly good reason why this can't work. Think of the havoc it would
wreak upon a vector if each contained element was a different size
(inevitable since your variant object includes a T object). Allocating
space for more than one object at a time would be impossible, random
access iterators would be nearly useless because you wouldn't know how
far ahead in memory to jump, etc.
Jul 23 '05 #3

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

Similar topics

8
by: Wolfgang Lipp | last post by:
<annotation> the first eleven contributions in this thread started as an off-list email discussion; i have posted them here with the consent of their authors. -- _w.lipp </annotation> From:...
8
by: Simon Elliott | last post by:
Given a template function: template <class T> void foo(T& value) { // do something with value } Is there any way I can tell whether the type T is an STL container?
3
by: deancoo | last post by:
Hello all, I'm new to the C++ STL, and am trying to wrap my head around why the following piece of code doesn't compile. What am I doing wrong? Thanks for any help. d #include <cstdlib>...
2
by: TadPole | last post by:
Hi all, My main problems are::::::::: 1. Set a value within a block container that can be used and changed by subsequent templates/block-containers/tables etc.. 2. get/determine/find the...
8
by: Ross A. Finlayson | last post by:
I'm trying to write some C code, but I want to use C++'s std::vector. Indeed, if the code is compiled as C++, I want the container to actually be std::vector, in this case of a collection of value...
23
by: Sanjay Kumar | last post by:
Folks, I am getting back into C++ after a long time and I have this simple question: How do pyou ass a STL container like say a vector or a map (to and from a function) ? function: ...
9
by: moleskyca1 | last post by:
I've ran into a problem with inheritance and STL containers. The code is too much to list, but here is the basic problem: #include <queue> using namespace std; class Base { public:
35
by: dragoncoder | last post by:
Just a simple theoritical question to the experts. What was the rationale behind making STL containers follow copy semantics rather than reference semantics. References almost always make things...
21
by: George Exarchakos | last post by:
Hi everyone, I'd like your help... Can we have a std::list<BASEwhere BASE be the base class of a class hierarchy? I want to add to this list objects that are inherited from BASE class but not...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.