Introduction:

Before the possibilities of the new C++ language standardC++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 easierclearer & 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:

1
2
template<typename... Arguments>
class VariadicTemplate;
 

Any of the following ways to create an instance of this class template is valid:

1
2
3
VariadicTemplate<double, float> instance;
VariadicTemplate<bool, unsigned short int, long> instance;
VariadicTemplate<char, std::vector<int>, std::string, std::string, std::vector<long long>> instance;
 

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:

1
2
template<typename T, typename... Arguments>
class VariadicTemplate;
 

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:

1
2
template<typename T = int, typename... Arguments>
class VariadicTemplate;
 

 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:

1
2
3
4
5
6
try{
// Try block.
}
catch(...){
// Catch block.
}
 

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:

1
2
template<typename... Arguments>
void SampleFunction(Arguments... parameters);
 

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<intint>(16, 24);

...an equivalent function template would be like this:

1
2
template<typename T, typename U>
void SampleFunction(T param1, U param2);
 

 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:

1
2
3
4
5
template<typename... Arguments>
class VariadicTemplate{
private:
static const unsigned short int size = sizeof...(Arguments);
};
 

 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:

1
2
3
4
template<typename... Arguments>
void SampleFunction(Arguments......){ }
 

As already stated, these two ellipsis operator put together can be written differently, so the following examples

1
2
3
4
template<typename... Arguments>
void SampleFunction(Arguments... ...){ }
 
1
2
3
4
template<typename... Arguments>
void SampleFunction(Arguments..., ...){ }
 

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:

1
2
template<typename... BaseClasses>
class VariadicTemplate : public BaseClasses...
 

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:

1
2
3
4
5
6
7
template<typename... BaseClasses>
class VariadicTemplate : public BaseClasses...{
public:
VariadicTemplate(BaseClasses&&... base_classes) : BaseClasses(base_classes)...{ }
};
 

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
template<typename T>
class Template{
public:
void SampleFunction(T param){ }
}; template<>
class Template<int>{
public:
void SampleFunction(int param){ }
};
 

But with variadic templates it's like the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
template<typename... Arguments>
class VariadicTemplate{
public:
void SampleFunction(Arguments... params){ }
}; template<>
class VariadicTemplate<double, int, long>{
public:
void SampleFunction(double param1, int param2, long param3){ }
};
 

 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(可变参数模板)的更多相关文章

  1. 【C/C++】C++11 Variadic Templates

    Variadic Templates 1.function template:利用“参数个数逐一递减”的特性,实现递归函数调用 template <typename T, typename... ...

  2. C++反射机制:可变参数模板实现C++反射(使用C++11的新特性--可变模版参数,只根据类的名字(字符串)创建类的实例。在Nebula高性能网络框架中大量应用)

    1. 概要   本文描述一个通过C++可变参数模板实现C++反射机制的方法.该方法非常实用,在Nebula高性能网络框架中大量应用,实现了非常强大的动态加载动态创建功能.Nebula框架在码云的仓库地 ...

  3. 泛化之美 —— C++11 可变参数模板的妙用

    概述 首先这篇文章出自博客园作者:[qicosmos ],我对本文的实例代码进行了学习.思考和整理纠正,理清了文章的全部细节,觉得这是一篇让我受益匪浅的文章.之所以会接触「可变参数模板」这部分的内容, ...

  4. c++11 可变参数模板类

    c++11 可变参数模板类 #define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <string> #inc ...

  5. c++11 可变参数模板函数

    c++11 可变参数模板函数 #define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <string> #in ...

  6. C++反射机制:可变参数模板实现C++反射

    1. 概要   本文描述一个通过C++可变参数模板实现C++反射机制的方法.该方法非常实用,在Nebula高性能网络框架中大量应用,实现了非常强大的动态加载动态创建功能.Nebula框架在Github ...

  7. 第27课 可变参数模板(8)_TupleHelper

    1. TupleHelper的主要功能 (1)打印:由于tuple中的元素是可变参数模板,外部并不知道内部到底是什么数据,有时调试时需要知道其具体值,希望能打印出tuple中所有的元素值. (2)根据 ...

  8. 第25课 可变参数模板(6)_function_traits和ScopeGuard的实现

    1. function_traits (1)function_traits的作用:获取函数的实际类型.返回值类型.参数个数和具体类型等.它能获取所有函数语义类型信息.可以获取普通函数.函数指针.std ...

  9. C++11_ Variadic Templates

    版权声明:本文为博主原创文章,未经博主允许不得转载. 这次主要介绍C++11的又一个新特性 Variadic Templates (可变模板参数) 它的实现类似于initializer_list< ...

随机推荐

  1. Wcf+EF框架搭建实例

    一.最近在使用Wcf创建数据服务,但是在和EF框架搭建数据访问时遇到了许多问题 下面是目前整理的基本框架代码,经供参考使用,源代码地址:http://git.oschina.net/tiama3798 ...

  2. js中如何把字符串转化为对象

    例如   [javascript]  var test='{ colkey: "col", colsinfo: "NameList" }'       var ...

  3. JNI类型与C/C++映射关系

    Java 类型 本地类型 描述 boolean jboolean C/C++8位整型 byte jbyte C/C++带符号的8位整型 char jchar C/C++无符号的16位整型 short ...

  4. java.io.FileNotFoundException: class path resource [META-INF/xfire/services.xml] cannot be opened because it does not exist

    解决办法: maven创建项目时: META-INF目录下面新建一个xfire文件夹,把services.xml文件放到这个文件夹里,再将整个META-INF拷贝到WEB-INF中 clean一下工程 ...

  5. iOS 网络与多线程--4.同步Post方式的网络请求

    通过Post请求方式,同步获取网络数据,一旦发送同步请求,程序将停止用户交互,直至服务器返回数据 在ViewController.m文件内的viewDidLoad函数添加一下测试代码 - (void) ...

  6. ASP.NET c#学习经验

    1.DataGrid自定义字段.<Column  <asp:BoundColumn DataField="khbh" HeaderText="客户编号&quo ...

  7. Dapper事务操作

    1.报错信息: 如果分配给命令的连接位于本地挂起事务中,ExecuteNonQuery 要求命令拥有事务.命令的 Transaction 属性尚未初始化. 出现这种原因是在执行Execute语句时,没 ...

  8. 学习OpenSeadragon之三 (覆盖层Overlayer的使用)

    Overlayer(覆盖层)是一个很重要的机制,它可以在可缩放图片上显示额外的信息. 1.简单应用 以下是我做出的一个小例子: 看这小老鼠头部的红色框内的部分就是一个分离出来的overlay. 介绍一 ...

  9. DropdownList控件绑定数据源显示system.data.datarowview的问题

    .net开发的时候经常需要用到在后台取数据再绑定到控件的问题,通常只需要连接数据库,从数据库取出数据,放到Dataset里面,然后再设置控件的DataSource为这个Dataset,然后再datab ...

  10. PHP5中使用PDO连接数据库

    PDO  如何连接 new PDO("mysql:host=localhost;dbname=php100","root", “ "); 默认这个不是 ...