template<class Key1, class Key2, class Type> class DualMultimapCache { public: std::list<std::reference_wrapper<Type>> get(Key1 const & key); std::list<std::reference_wrapper<Type>> get(Key2 const & key); template<class ...Args> Type & put(Key1 const & key, Args const & ...args); template<class ...Args> Type & put(Key2 const & key, Args const & ...args); };
Here I have an open interface for the class. The underlying data structures do not matter. Everything will work fine when Key1 and Key2 are of different types. If they end up being the same type, overloads are likely to be impossible. Am I correct in this?
If I, is there a way to split the overloads while keeping the signature as clean as possible?
EDIT: here's a more detailed example
template<class Key1, class Key2, class Type> class DualMultimapCache { public: std::list<std::reference_wrapper<Type>> get(Key1 const & key); std::list<std::reference_wrapper<Type>> get(Key2 const & key); template<class ...Args> Type & put(Key1 const & key, Args const & ...args); template<class ...Args> Type & put(Key2 const & key, Args const & ...args); private: std::unordered_multimap<Key1, std::reference_wrapper<Type>> map_Key1; std::unordered_multimap<Key2, std::reference_wrapper<Type>> map_Key2; }; template<class Key1, class Key2, class Type> std::list<std::reference_wrapper<Type>> DualMultimapCache<Key1, Key2, Type>::get(Key1 const & key) { auto its = map_Key1.equal_range(key); if (its.first == map.cend() && its.second == map.cend()) throw std::out_of_range(); else return { its.first, its.second }; } template<class Key1, class Key2, class Type> std::list<std::reference_wrapper<Type>> DualMultimapCache<Key1, Key2, Type>::get(Key2 const & key) { auto its = map_Key2.equal_range(key); if (its.first == map.cend() && its.second == map.cend()) throw std::out_of_range(); else return { its.first, its.second }; }
c ++ templates method-overloading overload-resolution class-template
Alexandre Parent
source share