I am trying to implement a card with different access keys using variable templates in C ++. I want to get this syntax work:
MultikeyMap<int, double, float> map1;
Now it looks like this for me:
template<class V, class... Krest> class MultikeyMap; template<class V, class K, class... Krest> class MultikeyMap<V, K, Krest...> : protected std::map<K, V>, protected MultikeyMap<V, Krest...> { public: template<class T> void insert( const T& t, const V& v ) { std::map<T, V>::insert( make_pair( t, v )); } template<class T> const V* find( const T& k ) { typedef std::map<T,V> currentMap; currentMap::const_iterator it = currentMap::find( k ); return it == currentMap::end() ? 0 : &it->second; } }; template<class V> class MultikeyMap<V> {};
I did not return iterators in the insert and found to make the code simple.
I see two main flaws in this solution.
First , the value type begins first in the template argument list. At first I tried to write
template<class K, class... Krest, class V> class MultikeyMap<K, Krest..., V>
but the compiler insists that "if the argument for partial specialization of the template template is a package extension, it should be the last argument .
The second is protected inheritance from std :: maps. I would really like to use the composition instead, but in this case, I see no way to access the saved maps. If static_if existed, I would write
template<class V, class K, class... Krest> class MultikeyMap<V, K, Krest...> : protected MultikeyMap<V, Krest...> { public: template<class T> void insert( const T& t, const V& v ) { static if( is_same<T,K>::value ) m_map.insert( make_pair( t, v )); else MultikeyMap<V, Krest...>::insert( t, v ); } private: std::map<K,V> m_map; };
I ask for advice on the issues addressed. If there is a better approach, I will be glad to know.
Thank you for reading.