ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

c – 从参数包中删除最后一个参数

2019-08-30 15:05:01  阅读:157  来源: 互联网

标签:c c11 templates variadic-templates


今天,在第100次使用相同的模板时,我想到了引入简单替换的想法.

代替

 QHash<int,QHash<int,QHash<int,int> > >

现在应该是:

 MultiKeyHash<int,int,int,int>

(不要与QT的QMultiHash类混淆).

前三个参数是键的类型,最后一个参数是值的类型.

 MultiKeyHash<TKey1,TKey2,....,TKeyN,TValue>

我目前的实现如下:

template <typename... Args>
struct MultiKeyHash;

template <typename TKey1, typename... Args>
struct MultiKeyHash<TKey1, Args...> : QHash<TKey1,MultiKeyHash<Args...> > {

    typedef typename MultiKeyHash<Args...>::ValueType ValueType;
    const ValueType& value(const TKey1 &key1, const Args_Without_Last&... args) const {
          return operator[](key1).value(args...);
    }
};

template <typename TKeyN, typename TValue>
struct MultiKeyHash<TKeyN,TValue> : QHash<TKeyN,TValue> {
    typedef TValue ValueType;
};

一切都按预期工作,直到我想添加“价值”方法.我当然不能使用“Args”作为第二个参数的类型(-pack).我是否必须制作它的功能模板,或者有办法吗?

我如何定义“值”方法,以便能够像下面这样调用它:

value(key1,key2,...,keyn)

解决方法:

这是MultiHashKey的工作版本.它使用QHash的虚拟实现进行测试.

#include <iostream>

// Dummy implementation of QHash for testing purposes.
template <typename TKey, typename TValue>
struct QHash
{
   void insert(const TKey& key, const TValue& val) {this->val = val;}
   const TValue& value(const TKey& key) const { return val; }
   TValue val;
};

template <typename... Args> struct MultiKeyHash;

template <typename TKey, typename... Args>
struct MultiKeyHash<TKey, Args...> : QHash<TKey, MultiKeyHash<Args...>>
{
   typedef TKey KeyType;
   typedef typename MultiKeyHash<Args...>::ValueType ValueType;
   typedef QHash<KeyType, MultiKeyHash<Args...>> QHashType;

   using QHashType::insert;
   using QHashType::value;

   MultiKeyHash() {}

   MultiKeyHash(const TKey &key, const Args&... args)
   {
      this->insert(key, MultiKeyHash<Args...>(args...));
   }

   void insert(const TKey &key, const Args&... args)
   {
      MultiKeyHash<Args...> val(args...);
      this->insert(key, val);
   }
   template <typename TKey1, typename ... Args1>
   const ValueType& value(const TKey1 &key1, const Args1&... args1) const
   {
      MultiKeyHash<Args...> const& val = this->value(key1);
      return val.value(args1...);
   }
};

template <typename TKey, typename TValue>
struct MultiKeyHash<TKey, TValue> : QHash<TKey, TValue> 
{
   typedef TKey KeyType;
   typedef TValue ValueType;
   typedef QHash<KeyType, ValueType> QHashType;

   MultiKeyHash() {}

   MultiKeyHash(const TKey &key, const TValue& val)
   {
      this->insert(key, val);
   }
};

void test1()
{
   MultiKeyHash<int, double> hash;
   hash.insert(10, 200.5);
   double v = hash.value(10);
   std::cout << "Value: " << v << std::endl;
}

void test3()
{
   MultiKeyHash<int, int, int, double> hash;
   hash.insert(10, 10, 10, 20.5);
   double v = hash.value(10, 10, 10);
   std::cout << "Value: " << v << std::endl;
}

int main()
{
   test1();
   test3();
   return 0;
};

运行程序的输出:

Value: 200.5
Value: 20.5

标签:c,c11,templates,variadic-templates
来源: https://codeday.me/bug/20190830/1769102.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有