473,616 Members | 2,970 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

map containing a string key and a templated value...

Hello,

I am trying to create a map/dictionary where the type of key is known
ie std::string, but the value could be of any built in type. ie. int,
double etc. (something along the lines of map<string, Twhere T is
template argument)

To hold the value type I have something like:

template<class T>
struct Data
{
T value;
}

Now I am trying to declare a map as below:

std::map<std::s tring, Data>;

this doesn't/wouldn't work and I don't know how to declare it??

if I have a method which has to pass in this map as a parameter, how
would I do it??
e.g Foo(std::map<st ring, Dataparam ) doesn't work and I don't expect
it to either

Can someone please help!

Aug 10 '06 #1
17 2206
de*******@hotma il.com schrieb:
Hello,

I am trying to create a map/dictionary where the type of key is known
ie std::string, but the value could be of any built in type. ie. int,
double etc. (something along the lines of map<string, Twhere T is
template argument)

To hold the value type I have something like:

template<class T>
struct Data
{
T value;
}

Now I am trying to declare a map as below:

std::map<std::s tring, Data>;

this doesn't/wouldn't work and I don't know how to declare it??
If you want a heterogenous container, you need run-time polymorphism;
you can't do it with templates alone.

That is, use a virtual base class and templated sub-classes which hold
the actual type.

The easiest way would be to use either of boost::any or
boost::variant< >, they both do something similar:

std::map<std::s tring, boost::anymapOf AnyType;

--
Thomas
Aug 10 '06 #2
Many Thanks for your prompt reply! Can I please ask following

1) will boost::any let me store any type including built-in types and
are there good examples on their site with regards to this particular
feature?
2) when you said "That is, use a virtual base class and templated
sub-classes which hold the actual type." - did you mean declaring a
something like map<string, base*>? would the base have to be declared
as a template too?

Aug 10 '06 #3

de*******@hotma il.com wrote:
Hello,

I am trying to create a map/dictionary where the type of key is known
ie std::string, but the value could be of any built in type. ie. int,
double etc. (something along the lines of map<string, Twhere T is
template argument)

To hold the value type I have something like:

template<class T>
struct Data
{
T value;
}

Now I am trying to declare a map as below:

std::map<std::s tring, Data>;

this doesn't/wouldn't work and I don't know how to declare it??

if I have a method which has to pass in this map as a parameter, how
would I do it??
e.g Foo(std::map<st ring, Dataparam ) doesn't work and I don't expect
it to either

Can someone please help!
I think you can use

std::map<std::s tring, Data*>;

Aug 10 '06 #4
de*******@hotma il.com schrieb:
Many Thanks for your prompt reply! Can I please ask following

1) will boost::any let me store any type including built-in types and
are there good examples on their site with regards to this particular
feature?
There are some restrictions for the types documented, but you can store
all built-in types. For the examples, look for yourself:

http://www.boost.org/doc/html/any.html
2) when you said "That is, use a virtual base class and templated
sub-classes which hold the actual type." - did you mean declaring a
something like map<string, base*>? would the base have to be declared
as a template too?
Yes, I meant something like that, and no, the base class should better
not be a template. You can't put a templated class or pointer to
templated class in a container, you can do this only with a concrete
instantiation of the template. That is why you need a base class.

The boost::any class does this for you, and it works as a simple smart
pointer, so you don't even have to store raw pointers in the map.

--
Thomas
Aug 10 '06 #5
zh**********@gm ail.com wrote:
de*******@hotma il.com wrote:
Hello,

I am trying to create a map/dictionary where the type of key is known
ie std::string, but the value could be of any built in type. ie. int,
double etc. (something along the lines of map<string, Twhere T is
template argument)
[...]
>
I think you can use

std::map<std::s tring, Data*>;
No. Data is not a type. It's a template. Any instantiation of the Data
template,
e.g. Data<int>, is a type. The arguments to the std::map template are
two
types. [1] Hence, you can write std::map<std::s tring, Data<int
MyMap;
With the proposed Data template that of course adds little value.

The better solution is already given. (Use boost)

HTH,
Michiel Salters

[1] Not counting the third argument, because it has a default
(std::allocator )

Aug 10 '06 #6
Looks like there is really no way in C++ to have a generic type like
that in a map. Templates solve half the problem but don't go all the
way.. :-(

it is not possible to use std::map<std::s tring, Data*for the reasons
mentioned by Michiel.
What I can do is make Data a base class, then derive templated class
from data and instantiate these, and store pointers to these in the
above map. All sounds like too much effort!
I like the idea of using boost but I am not allowed to use it in my
project :-(

Thanks to all of you again...

Aug 10 '06 #7
de*******@hotma il.com wrote:
Hello,

I am trying to create a map/dictionary where the type of key is known
ie std::string, but the value could be of any built in type. ie. int,
double etc. (something along the lines of map<string, Twhere T is
template argument)

To hold the value type I have something like:

template<class T>
struct Data
{
T value;
}

Now I am trying to declare a map as below:

std::map<std::s tring, Data>;

this doesn't/wouldn't work and I don't know how to declare it??

if I have a method which has to pass in this map as a parameter, how
would I do it??
e.g Foo(std::map<st ring, Dataparam ) doesn't work and I don't expect
it to either

Can someone please help!
Just to be clear: do you want to store actual values of different types
in the same map (if so, use boost::any as suggested by others), or are
you just wanting to have a "template typedef" that defines only one of
the existing tempalte parameters like this:

template<typena me T>
struct MyMap
{
typedef std::map<std::s tring,Ttype;
};

MyMap<int>::typ e intMap;
MyMap<float>::t ype floatMap;

Cheers! --M

Aug 10 '06 #8
de*******@hotma il.com wrote:
[...] Templates solve half the problem but don't go all the
way.. :-(

[...] All sounds like too much effort!
Life... First you suffer through it, then you die...
Aug 10 '06 #9
Rather die than use VB or C# :-P

To the previous poster, no I did think of template typedef but that
only works if i want one specific type. I want a map which stores any
built in data type ie. int, double etc...
so something like map<string, object) in java or other similar
languages...

Victor Bazarov wrote:
de*******@hotma il.com wrote:
[...] Templates solve half the problem but don't go all the
way.. :-(

[...] All sounds like too much effort!

Life... First you suffer through it, then you die...
Aug 10 '06 #10

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

Similar topics

2
3086
by: Thomas Matthews | last post by:
Hi, I'm working with Borland C++ Builder 6.2. My project uses the std::string class. However, Borland in its infinite wisdom has its own string class, AnsiString. To make my life easier, I want to create a function, preferably a conversion operator, to convert from a std::string to an AnsiString. Please note that I don't want to change either std::string or AnsiString.
9
2305
by: Jon Wilson | last post by:
I have a class which needs to accumulate data. The way we get this data is by calling a member function which returns float on a number of different objects of different type (they are all the same type for a given instance of the class, but different types for different instances.) #include<set> using namespace std; template<class T>
2
1495
by: Alex Drummond | last post by:
Hello, Is there any way of specializing a templated function on a type which is itself templated? Here's the simplest example of the problem I can think of. Say I have written an implementation of the identity function as follows, template <class T> T identity(T x) { return x; } and for some reason I want to specialize this function for the
6
1521
by: Dan Huantes | last post by:
I was presented a problem today where a class had member variable that was an object of a templated class. The class wanted to instantiate the object as a private member variable and call a constructor other than the default constructor. I couldn't figure out why it wasn't working so I changed the member variable to a pointer to an object instead and intialized it in the constructor and destroyed in the destructor. It bothered me that...
0
357
by: John Crowley | last post by:
I keep running into this over and over again... I want a block server control that renders a header and footer, and child controls in between. But I don't want a templated control, for the following reasons: 1) Render blocks are not allowed inside a templated control. 2) I want the inner controls scoped at the parent aspx (or ascx), not at the template naming container. This type of control would be ideal for website headers and...
6
1307
by: Alex | last post by:
I have been loving the templated datacolumns of the datagrid. I have stumbled onto a problem though that is beyond by knowledge and I was hoping someone here could jumpstart me. My templated columns work find as long as I pass in a dataset but if I pass in a collection of objects then they won't work unless I can cast the correct object and then fill in the column with the object. My current code looks like this: public void...
2
2923
by: domehead100 | last post by:
I have a templated class, CDerived: template <typename TValue, typename TDraw, typename TEdit ...> class CDerived : public CBase { TValue m_Value public: TValue& GetValue() const {
14
1283
by: Christopher | last post by:
Related to my last post, but seperate question Can a class be made into a container of its self? Could I Create an attribute class representing a name, value pair Create an attributeGroup class wrapping a map of attributes and then add to the attributeGroup class to also wrap a map of attributeGroups?
2
2280
card
by: card | last post by:
Hi everyone, I have a question about referencing a nested class contained within a templated class. Of course the best way to show you is by example. Here's my templated classes: #include <stack> template <class T> class A { public:
0
8146
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8647
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
8592
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
7121
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5550
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
4063
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4141
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2579
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1445
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.