7
  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 };
  }
4

2 回答 2

3

您可以针对相同密钥类型的情况部分专门化模板,例如

template <typename Key, typename Type>
class DualMultimapCache<Key, Key, Type>
{
public:
   std::list<std::reference_wrapper<Type>> get(Key const & key);
   template<class ...Args> Type & put(Key const & key, Args const & ...args);
};
于 2017-12-11T14:32:14.877 回答
0

我认为您必须使用带有 2 个参数的偏特化,但这并不方便,因为您必须使用稍微不同的界面。为了解决这个问题,我建议使用 SFINAE

template<typename Key1, typename Key2, typename Type,
     typename Enable = void > 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);
};

template<typename Key1, typename Key2, typename Type > class DualMultimapCache < Key1, Key2, Type,
typename std::enable_if<std::is_same<Key1, Key2>::value>::type >
{
public:
    std::list<std::reference_wrapper<Type>> get(Key1 const & key);
    template<class ...Args> Type & put(Key1 const & key, Args const & ...args);
};

您可以使用具有 3 个模板参数的单一界面,但获得不同的专业化:

DualMultimapCache<int, double, int> t1; // DualMultimapCache template impl
DualMultimapCache<int, int, int> t2;    // DualMultimapCache template 
                                        // specialization impl
于 2017-12-11T15:04:21.213 回答