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);
};
在这里,我有一个类的公共接口。底层数据结构无关紧要。Key1
当并且Key2
是不同类型时,一切都会正常工作。如果它们最终是相同的类型,那么重载可能是不可能的。我这样想对吗?
如果我是,有没有办法在保持签名尽可能干净的同时分离重载?
编辑:这里有一个更深入的样本
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 };
}