C++11 : variadic templates(可变参数模板)
Introduction:
Before the possibilities of the new C++ language standard, C++11, the use of templates was quite limited when it came to implementing for instance function objects (functors) & tuple facilities. Implementing these sort of things using earlier C++ standard often require similiar code to be repeated various times without forgetting preprocessor metaprogramming. However, thanks to variadic templates, programming new features using templates has become easier, clearer & more memory-efficient.
Although the D programming language also provides the use of variadic templates, only variadic templates offered by C++11 standard will be covered here, so knowledge of D programming language's variadic templates is not required in order to read & understand this article. There are assumptions, however, that the reader of this article understands what class & function templates are & how to declare, define & use them.
What is a variadic template?
Variadic template is a template, which can take an arbitrary number of template arguments of any type. Both the classes & functions can be variadic. Here's a variadic class template:
|
|
Any of the following ways to create an instance of this class template is valid:
|
|
The number of variadic template arguments can also be zero, so the following
VariadicTemplate<> instance;
is also valid C++11.
However, if you create a template like this:
|
|
You must set at least one type as a template argument (for typename T), unless default type has been initilialized, like in the following declaration:
|
|
Syntax - the ellipsis operator (...):
The ellipsis operator (...) is an operator used in different contexts in C++. It's name comes from an ellipsis mechanism in C. In this mechanism programmer can create a function taking variable number of parameters. Probably the most famous function in both C & C++ to take advantage of this mechanism is printf-function in C standard library:
int printf (const char* format, ... );
Ellipsis mechanism can also be used with preprocessor in a form of a macro. A macro taking a variable number of parameters is called a variadic macro.
#define VARIADIC_MACRO(...)
In C++, this ellipsis operator got a new meaning in different context called exception handling. The operator is used in catch blocks after try blocks:
|
|
Here, the ellipsis operator indicates that the catch block takes in any exception thrown from the try block as it's parameter, no matter the type.
In C++11, variadic templates brought yet another meaning for this operator. The operator works somewhat like in ellipsis mechanism as already stated, but it's bit more complex:
|
|
Here's a function template. The contents of the variadic template arguments are called parameter packs. These packs will then be unpacked inside the function parameters. For example, if you create a function call to the previous variadic function template...
SampleFunction<int, int>(16, 24);
...an equivalent function template would be like this:
|
|
Syntax - the sizeof... operator (sizeof...):
Another operator used with variadic templates is the sizeof...-operator. Unlike the sizeof operator, which can be used to determine the size of a type (for example sizeof(int) or sizeof(double)), sizeof... operator can be used to determine the amount of types given into a variadic template. This can be achieved like this:
|
|
Syntax - two ellipsis operators together (......):
In some circumstances, there can be two ellipsis operators put together (......). These two operators can also be separated (written as ... ...).
Probably the most clear way, however, is to separate these two operators with a comma (..., ...). Both ways with a comma or without a comma are acceptable.
This kind of syntax can appear with variadic function templates using ellipsis mechanism:
|
|
As already stated, these two ellipsis operator put together can be written differently, so the following examples
|
|
|
|
work as well.
Author's opinions & thoughts: For the sake of readability, use the last method to mark the two following ellipsis operators. The previous alternatives may be found confusing and/or cumbersome. Some may find it a matter of taste, though.
Uses of variadic templates - inheritance & initialization lists:
When it comes to classes, variadic templates can be used with inheritance & initialization lists. Inheritance taking advantage of variadic templates can be accomplished like this:
|
|
And, if we want to create a constructor inside this class using initialization list to call the constructors of all the given base classes as template arguments, we'd have to do it this way:
|
|
As you can see there's a new operator introduced in C++11 in the constructor's parameter list - an rvalue operator (&&), which allows rvalue references. This article is not intended to cover the use of this operator, but for information how to use this operator (& rvalue references in general), please follow this link:
http://thbecker.net/articles/rvalue_references/section_01.html
Uses of variadic templates - variadic class template specialization:
Like class templates, variadic class templates can also be specialized. With templates, the specialization happens like this:
|
|
But with variadic templates it's like the following:
|
|
Caution: Some compilers may not support variadic class template specialization yet, or their implementation may be somewhat incomplete.
See also:
If you are interested in seeing a C++11 standard class template utilizing variadic templates, please take a look at already mentioned tuple from the link below:
http://www.cplusplus.com/reference/std/tuple/tuple/
Another field where variadic templates may come in handy is delegates. If you are already familiar with managed C++ and/or C#, picking up C++ delegates may not be a problem. You might find good use for them in C++ anyway.
C++11 : variadic templates(可变参数模板)的更多相关文章
- 【C/C++】C++11 Variadic Templates
Variadic Templates 1.function template:利用“参数个数逐一递减”的特性,实现递归函数调用 template <typename T, typename... ...
- C++反射机制:可变参数模板实现C++反射(使用C++11的新特性--可变模版参数,只根据类的名字(字符串)创建类的实例。在Nebula高性能网络框架中大量应用)
1. 概要 本文描述一个通过C++可变参数模板实现C++反射机制的方法.该方法非常实用,在Nebula高性能网络框架中大量应用,实现了非常强大的动态加载动态创建功能.Nebula框架在码云的仓库地 ...
- 泛化之美 —— C++11 可变参数模板的妙用
概述 首先这篇文章出自博客园作者:[qicosmos ],我对本文的实例代码进行了学习.思考和整理纠正,理清了文章的全部细节,觉得这是一篇让我受益匪浅的文章.之所以会接触「可变参数模板」这部分的内容, ...
- c++11 可变参数模板类
c++11 可变参数模板类 #define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <string> #inc ...
- c++11 可变参数模板函数
c++11 可变参数模板函数 #define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <string> #in ...
- C++反射机制:可变参数模板实现C++反射
1. 概要 本文描述一个通过C++可变参数模板实现C++反射机制的方法.该方法非常实用,在Nebula高性能网络框架中大量应用,实现了非常强大的动态加载动态创建功能.Nebula框架在Github ...
- 第27课 可变参数模板(8)_TupleHelper
1. TupleHelper的主要功能 (1)打印:由于tuple中的元素是可变参数模板,外部并不知道内部到底是什么数据,有时调试时需要知道其具体值,希望能打印出tuple中所有的元素值. (2)根据 ...
- 第25课 可变参数模板(6)_function_traits和ScopeGuard的实现
1. function_traits (1)function_traits的作用:获取函数的实际类型.返回值类型.参数个数和具体类型等.它能获取所有函数语义类型信息.可以获取普通函数.函数指针.std ...
- C++11_ Variadic Templates
版权声明:本文为博主原创文章,未经博主允许不得转载. 这次主要介绍C++11的又一个新特性 Variadic Templates (可变模板参数) 它的实现类似于initializer_list< ...
随机推荐
- SAE下的Memcache使用方法
SAE里面有Memcache,可以较大幅度改善数据库的鸭梨~ 之前一直想学习Memcache,却愁于不知如何下手,对这个名词完全没有概念,同时在SAE的文档里面,也很少对于Memcache的使用教程~ ...
- 网络断开后重连downloadProvider继续下载问题调试分析
最近在安卓4.4上遇到一个断开wifi后重新连接wifi, downloadProvider继续下载文件失败的问题.于是开始了解下载管理模块的断点续载功能: 1.首先,分析android lo ...
- BIND9详解之日志篇
在默认情况下,BIND把日志消息写到/var/log/messages文件中,而这些日志消息是非常少的,主要就是启动,关闭的日志记录和一些严重错误的消息,所以要详细记录服务器的运行状况,需要自己配置服 ...
- 网络编程 socket-实例
1.设计界面: 2.效果界面: 3.具体实现代码: public partial class frmMain : Form { public frmMain() { InitializeC ...
- [转]机器学习——C4.5 决策树算法学习
1. 算法背景介绍 分类树(决策树)是一种十分常用的分类方法.它是一种监管学习,所谓监管学习说白了很简单,就是给定一堆样本,每个样本都有一组属性和一个类别,这些类别是事先确定的,那么通过学习得到一个分 ...
- 2014年企业改善IT风险管理的5个办法
进入新的一年,企业面对数据泄密事故.日益复杂的攻击和对其控制的持续监管,现在是时候重新审视其风险管理战略了.虽然每个企业都是独特的,风险管理专家认为有些风险管理办法值得企业关注.下面我们列出了5个风险 ...
- VS Code 项目编译
编译运行vs code源码 如果我们想本地运行 vs code 需要安装一些必要的库. 我们需要安装 'Node.JS' . 这里需要注意的是,最低版本要求是'5.1.0'. 还需要安装 'Pytho ...
- linux下实现ls()函数遍历目录
转载请注明原创:http://www.cnblogs.com/StartoverX/p/4600794.html 需求:在linux下遍历目录,输出目录中各文件名. 在linux下遍历目录的相关函数有 ...
- OpenStack虚机相关错误
OpenStack配置起来还是挺麻烦的,特别是网络那块.虽然官方文档越来越清晰,但有时还是会出各种错.排错主要是看日志.看官方文档和google 以下就一些虚机相关常见的错误做一下总结(基于Iceho ...
- 启动任务StartTask() 发送完消息队列 自己删除,接收方一直显示数据 用OSQFlush(Str_Q); //清空消息队列 下面纠结接收不到了 哈哈
在建立工程的时候,启动任务StartTask() 启动了任务MyTask(),也建立了消息队列,然后发送消息队列,发送完自己删除了自己,在接收方一直能接受到数据???为何??? 因为我们的消息队列未 ...