博客
关于我
C++11——variadic template
阅读量:281 次
发布时间:2019-03-01

本文共 2396 字,大约阅读时间需要 7 分钟。

例1:

//例1:void print()//递归出口{   	cout << "递归出口" << endl;}template
//版本1void print(const T&firstArg, const Types&...args){ cout << firstArg << endl; print(args...);}template
//版本2void print(const Types&...args){ cout << "test" << endl; print(args...);}//注意:版本1比版本2更特化,所以版本2永远不会被调用int main(){ print(7.5, "hello", bitset<16>(377), 42); system("pause"); return 0;}

在这里插入图片描述

例2:简单实现形参为initializer_list的max函数

//例2template
_ForwardIterator_my_max_element(_ForwardIterator _first, _ForwardIterator _last,_Compare cmp){ if (_first == _last)return _first; _ForwardIterator _result = _first; while (++_first != _last) { if (cmp(_result, _first)) _result = _first; } return _result;}//自定义比较函数functorclass cmp{ public: template
bool operator()(_Iterator1 _it1, _Iterator2 _it2)const { return *_it1 < *_it2; }};template
inline _ForwardIteratormy_max_element(_ForwardIterator _first, _ForwardIterator _last){ return _my_max_element(_first, _last, cmp());}template
inline _T my_max(initializer_list<_T>_l){ return *my_max_element(_l.begin(), _l.end());}int main(){ cout << my_max({ 1,2,5,3,7,9,5 }) << endl; cout << max({ 1,2,5,3,7,9,5 }) << endl;//这是标准库中的版本 system("pause"); return 0;}

例3:利用可变参数模板实现例2的效果

int maximum(int n)//只有1个参数时调用这个版本,是递归的出口{   	return n;}template
int maximum(int n, Args... args)//至少2个参数才可以调用这个版本{ return max(n, maximum(args...));}int main(){ cout << maximum(1, 2, 5, 3, 7, 9, 5) << endl; system("pause"); return 0;}

例4:使用可变参数模板简单实现tuple

在这里插入图片描述

对于上图的代码,要改一个错误的地方,见下图的黄标处。
在这里插入图片描述
这张图看明白后发现真的是经典!

#include
#include
#include
#include
#include
using namespace std;template
class my_tuple;//版本1template<>class my_tuple<> { };//版本1对应的全特化,作为递归的出口template
//比版本1更特化的版本2class my_tuple
:private my_tuple
{ public: //default constructor my_tuple() { } //constructor my_tuple(Head v, Tail... vtail) :m_head(v), my_tuple
(vtail...) { }//初始化列表中调用了基类的constructor Head head() { return m_head; } my_tuple
& tail() { return *this; }//返回的是直接基类的对象(派生类对象赋给一个基类引用)protected: Head m_head;};int main(){ my_tuple
t(41, 6.3, "nico"); cout << t.head() << endl;//41 cout << t.tail().head() << endl;//6.3 cout << t.tail().tail().head() << endl;//nico system("pause"); return 0;}

在这里插入图片描述

当然,上面这个例子是利用了继承的方式创建,其实也可以用嵌套(复合)的方式:
在这里插入图片描述

转载地址:http://zamt.baihongyu.com/

你可能感兴趣的文章
mysql 更新子表_mysql 在update中实现子查询的方式
查看>>
MySQL 有什么优点?
查看>>
mysql 权限整理记录
查看>>
mysql 权限登录问题:ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: YES)
查看>>
MYSQL 查看最大连接数和修改最大连接数
查看>>
MySQL 查看有哪些表
查看>>
mysql 查看锁_阿里/美团/字节面试官必问的Mysql锁机制,你真的明白吗
查看>>
MySql 查询以逗号分隔的字符串的方法(正则)
查看>>
MySQL 查询优化:提速查询效率的13大秘籍(避免使用SELECT 、分页查询的优化、合理使用连接、子查询的优化)(上)
查看>>
mysql 查询数据库所有表的字段信息
查看>>
【Java基础】什么是面向对象?
查看>>
mysql 查询,正数降序排序,负数升序排序
查看>>
MySQL 树形结构 根据指定节点 获取其下属的所有子节点(包含路径上的枝干节点和叶子节点)...
查看>>
mysql 死锁 Deadlock found when trying to get lock; try restarting transaction
查看>>
mysql 死锁(先delete 后insert)日志分析
查看>>
MySQL 死锁了,怎么办?
查看>>
MySQL 深度分页性能急剧下降,该如何优化?
查看>>
MySQL 深度分页性能急剧下降,该如何优化?
查看>>
MySQL 添加列,修改列,删除列
查看>>
mysql 添加索引
查看>>