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