Variadic Templates
Encyclopedia
In computer programming
, variadic templates are template
s that take a variable number of arguments.
Variadic templates are supported by the D programming language
, and the newest version of C++
, formalized in the C++11 standard.
This template class tuple will take any number of typenames as its template parameters:
The number of arguments can be zero, so
If one does not want to have a variadic template that takes 0 arguments, then this definition will work as well:
Variadic templates may also apply to functions, thus not only providing a type-safe add-on to variadic functions (such as printf) - but also allowing a printf-like function to process non-trivial objects.
The ... operator has two roles. When it occurs to the left of the name of a parameter, it declares a parameter pack. By using the parameter pack, user can bind zero or more arguments to the variadic template parameters. Parameter packs can also be used for non-type parameters. By contrast, when the ... operator occurs to the right of a template or function call argument, it unpacks the parameter packs into separate arguments, like the
The use of variadic templates is often recursive. The variadic parameters themselves are not readily available to the implementation of a function or class. Therefore, the typical mechanism for defining something like a C++11 variadic printf replacement would be as follows:
This is a recursive template. Notice that the variadic template version of printf calls itself, or (in the event that args... is empty) calls the base case.
There is no simple mechanism to iterate over the values of the variadic template. There are few methods to translate the argument pack into single argument use. Usually this will rely on function overloading, or - if your function can simply pick one argument at a time - using a dumb expansion marker:
This way you can use it:
which will expand to something like:
The use of this "pass" function is necessary because the argument packs expands with separating by comma, but it can only be a comma of separating the function call arguments, not an "operator," function. Because of that "some_function(args)...;" will never work. Moreover, this above solution will only work when some_function return type isn't void.
Another method is to use overloading with "termination versions" of functions. This method is more universal, but requires a bit more code and more effort to create. One function receives one argument of some type and the argument pack, the other does not have any of these two (beside this both may have the same list of initial parameters - in this example there won't be any):
If args... contains at least one argument, it will redirect to the second version - parameter pack can be also empty, so if it's empty, it will simply redirect to the termination version, which will do nothing.
Variadic templates can be used also in exception specification, base class list and constructor's initialization list. For example, a class can specify the following:
The unpack operator will replicate the types for the base classes of
With regard to function templates, the variadic parameters can be forwarded. When combined with rvalue references (see above), this allows for perfect forwarding:
This unpacks the argument list into the constructor of TypeToConstruct. The
Additionally, the number of arguments in a template parameter pack can be determined as follows:
The syntax
See also
External links
Computer programming
Computer programming is the process of designing, writing, testing, debugging, and maintaining the source code of computer programs. This source code is written in one or more programming languages. The purpose of programming is to create a program that performs specific operations or exhibits a...
, variadic templates are template
Template (programming)
Templates are a feature of the C++ programming language that allow functions and classes to operate with generic types. This allows a function or class to work on many different data types without being rewritten for each one....
s that take a variable number of arguments.
Variadic templates are supported by the D programming language
D (programming language)
The D programming language is an object-oriented, imperative, multi-paradigm, system programming language created by Walter Bright of Digital Mars. It originated as a re-engineering of C++, but even though it is mainly influenced by that language, it is not a variant of C++...
, and the newest version of C++
C++
C++ is a statically typed, free-form, multi-paradigm, compiled, general-purpose programming language. It is regarded as an intermediate-level language, as it comprises a combination of both high-level and low-level language features. It was developed by Bjarne Stroustrup starting in 1979 at Bell...
, formalized in the C++11 standard.
C++11
Prior to C++11, templates (classes and functions) can only take a fixed number of arguments that have to be specified when a template is first declared. C++11 allows template definitions to take an arbitrary number of arguments of any type.This template class tuple will take any number of typenames as its template parameters:
The number of arguments can be zero, so
tuple<> some_instance_name;
will work as well.If one does not want to have a variadic template that takes 0 arguments, then this definition will work as well:
Variadic templates may also apply to functions, thus not only providing a type-safe add-on to variadic functions (such as printf) - but also allowing a printf-like function to process non-trivial objects.
The ... operator has two roles. When it occurs to the left of the name of a parameter, it declares a parameter pack. By using the parameter pack, user can bind zero or more arguments to the variadic template parameters. Parameter packs can also be used for non-type parameters. By contrast, when the ... operator occurs to the right of a template or function call argument, it unpacks the parameter packs into separate arguments, like the
args...
in the body of printf
below. In practice, the use of ... operator in the code causes that the whole expression that precedes the ... operator, will be repeated for every next argument unpacked from the argument pack, and all these expressions will be separated by a comma.The use of variadic templates is often recursive. The variadic parameters themselves are not readily available to the implementation of a function or class. Therefore, the typical mechanism for defining something like a C++11 variadic printf replacement would be as follows:
This is a recursive template. Notice that the variadic template version of printf calls itself, or (in the event that args... is empty) calls the base case.
There is no simple mechanism to iterate over the values of the variadic template. There are few methods to translate the argument pack into single argument use. Usually this will rely on function overloading, or - if your function can simply pick one argument at a time - using a dumb expansion marker:
This way you can use it:
which will expand to something like:
The use of this "pass" function is necessary because the argument packs expands with separating by comma, but it can only be a comma of separating the function call arguments, not an "operator," function. Because of that "some_function(args)...;" will never work. Moreover, this above solution will only work when some_function return type isn't void.
Another method is to use overloading with "termination versions" of functions. This method is more universal, but requires a bit more code and more effort to create. One function receives one argument of some type and the argument pack, the other does not have any of these two (beside this both may have the same list of initial parameters - in this example there won't be any):
If args... contains at least one argument, it will redirect to the second version - parameter pack can be also empty, so if it's empty, it will simply redirect to the termination version, which will do nothing.
Variadic templates can be used also in exception specification, base class list and constructor's initialization list. For example, a class can specify the following:
The unpack operator will replicate the types for the base classes of
ClassName
, such that this class will be derived from each of the types passed in. Also, the constructor must take a reference to each base class, so as to initialize the base classes of ClassName
.With regard to function templates, the variadic parameters can be forwarded. When combined with rvalue references (see above), this allows for perfect forwarding:
This unpacks the argument list into the constructor of TypeToConstruct. The
std::forward(params)
syntax is the syntax that perfectly forwards arguments as their proper types, even with regard to rvalue-ness, to the constructor. The unpack operator will propagate the forwarding syntax to each parameter. This particular factory function automatically wraps the allocated memory in a std::shared_ptr
for a degree of safety with regard to memory leaks.Additionally, the number of arguments in a template parameter pack can be determined as follows:
The syntax
SomeStruct::size
will be 2, while SomeStruct<>::size
will be 0.See also
For articles on variadic constructs other than templates
- Variadic functionVariadic functionIn computer programming, a variadic function is a function of indefinite arity, i.e., one which accepts a variable number of arguments. Support for variadic functions differs widely among programming languages....
- Variadic macroVariadic macroA variadic macro is a feature of the C preprocessor whereby a macro may be declared to accept a varying number of arguments.Variable-argument macros were introduced in the ISO/IEC 9899:1999 revision of the C programming language standard in 1999...
in the C preprocessorC preprocessorThe C preprocessor is the preprocessor for the C and C++ computer programming languages. The preprocessor handles directives for source file inclusion , macro definitions , and conditional inclusion ....
External links
Variadic function
In computer programming, a variadic function is a function of indefinite arity, i.e., one which accepts a variable number of arguments. Support for variadic functions differs widely among programming languages....
Variadic macro
A variadic macro is a feature of the C preprocessor whereby a macro may be declared to accept a varying number of arguments.Variable-argument macros were introduced in the ISO/IEC 9899:1999 revision of the C programming language standard in 1999...
in the C preprocessor
C preprocessor
The C preprocessor is the preprocessor for the C and C++ computer programming languages. The preprocessor handles directives for source file inclusion , macro definitions , and conditional inclusion ....