making a variety map using templates

I was trying to replicate Python's dictionary, where its like c++'s map, for the exception that any data type can be a key or value.

In c++ however i have to define the type for key and value. But what if i "didn't" know, or wantedc to combine multiple maps into one?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <string>
#include <vector>
#include <map>

template <class T>
T dict(T, T){
    std::map<T, T> map;
    return map;
}


int main(){
    std::map<std::string, std::string> string_map;
    string_map = dict("key", "value");
    
    std::map<std::string, int> int_map;
    int_map = dict("key", 0);
}



So i am trying to play with it to get dict() function to "make" a map of any type, but apparently i dont have quite down templates yet.
Python is an interpreted language, having this flexibility in C++ comes at a cost.

There is something called a variant datatype various libraries provide, but it comes at a huge performance cost.

Perhaps you should consider the design of the program into a more C++ orientated design rather than thinking from a Python perspective.

Or even having a class with various members of different data types and having it in a data structure like a map.

Perhaps if you're just dealing with primitive data types and std::string you could store everything as a string and convert it. Although I highly discourage unless dealing with files.
yeah i have heard i have that problem. For the past few years i have been in heavily into Python, and a lot of people have said that i try to morph c++ into python.

But i figure Python is written in C, so i should be able to "recreate" it if i wanted via c++. I am just testing the limits.

Perhaps you should consider the design of the program into a more C++ orientated design

How would you implement Python dictionary style into c++ orientated design?
1
2
3
4
5
template <class T>
T dict(T, T){
    std::map<T, T> map;
    return map;
}


Should look something like this:
1
2
3
4
5
6
7
8
template<class K, class T>
std::map<K, T> dict()
{
   return std::map<K,T>(); //K is your key and T is your data type
} 

//Called like:
dict<KeyType, DataType>();


Although this is completely useless, since it's a needless wrapper around the std::map constructor.

Something with a little more substance to it would be:
1
2
3
4
5
6
7
template<class K, class T>
std::map<K, T> dict(K key, T data)
{
   std::map<K,T> map;
   map[key] = data;
   return map;
} 


Although again, this is a simple wrapper.

(Take this code with a grain of salt, I haven't codded in a while and don't have a compiler handy)

EDIT:
Concerning your actual question, you could look into creating a class which wraps up a union of all the types you want to support, and an enumeration which describes those types. Remember that you'll need to define a number of operators for it to be used with the std::map though.

EDIT 2: You best bet though is to forget this idea all together. Like Krisando said, C++ is strongly typed and doesn't directly support variant datatypes.
How would you implement Python dictionary style into c++ orientated design?

You wouldn't. Hence the addition of
rather than thinking from a Python perspective.
Last edited on
Topic archived. No new replies allowed.