C++1. 1 - New features - Variadic template. Introduction. Before the possibilities of the new C++ language standard, C++1. 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++1.
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.
Arguments>. class Variadic. Template; Any of the following ways to create an instance of this class template is valid: 1. Variadic. Template< double, float> instance. Variadic. Template< bool, unsignedshortint, long> instance. Variadic. Template< char, std: :vector< int> , std: :string, std: :string, std: :vector< longlong> > instance; The number of variadic template arguments can also be zero, so the following. Variadic. Template< > instance; is also valid C++1. However, if you create a template like this: 1.
T, typename.. Arguments>. Variadic. Template; 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. T = int, typename.. Arguments>. class Variadic. Template; 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 (constchar* format, .. A macro taking a variable number of parameters is called a variadic macro.#define VARIADIC. The operator is used in catch blocks after try blocks: 1. The operator works somewhat like in ellipsis mechanism as already stated, but it's bit more complex: 1. Arguments>. void Sample.
Function(Arguments.. 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.. Sample. Function< int, int> (1.
T, typename U>. Sample. Function(T param. U param. 2); Syntax - the sizeof.. Unlike the sizeof operator, which can be used to determine the size of a type (for example sizeof(int) or sizeof(double)), sizeof..
This can be achieved like this: 1. Arguments>. class Variadic. Template. These two operators can also be separated (written as .. Both ways with a comma or without a comma are acceptable.
Template function as a template argument. Unfortunately, you cannot pass template template function as a type, because it has to be instantiated first. Why Not Specialize Function Templates? Which function base template gets selected depends on which matches best and is the 'most specialized'.
This kind of syntax can appear with variadic function templates using ellipsis mechanism: 1. Arguments>. void Sample. Function(Arguments...).
Arguments>. void Sample. Function(Arguments.. Arguments>. void Sample. Function(Arguments.., ..).
Template Function Argument Definition
You can also create a proxy if you actually have a template that takes only one argument.
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.
Base. Classes>. Variadic. Template : public Base. Classes.. 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. Base. Classes>. Variadic. Template : public Base. Classes.. 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.
With templates, the specialization happens like this: 1. T>. class Template. Arguments>. class Variadic. Template. 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.
Conclusion. Templates have been a powerful feature in C++. Now, after the introduction of variadic templates, templates have proven themselves even more powerful. Variadic templates are a trustworthy solution to implement delegates and tuples. And, instead of C- style ellipsis mechanism, variadic templates can offer a typesafer solution to replace them. More information - compiler support.
If you are unfamiliar or simply unaware of your compiler's feature support, please visit this link: http: //wiki. C++0x. Compiler. Support. Special thanks to. Sources: Text sources: http: //digitalmars.