Variadic templateIn computer programming, variadic templates are templates that take a variable number of arguments. Variadic templates are supported by C++ (since the C++11 standard), and the D programming language. C++The variadic template feature of C++ was designed by Douglas Gregor and Jaakko Järvi [1][2] and was later standardized in C++11. Prior to C++11, templates (classes and functions) could only take a fixed number of arguments, which had to be specified when a template was first declared. C++11 allows template definitions to take an arbitrary number of arguments of any type. template<typename... Values> class tuple; // takes zero or more arguments
The above template class tuple<int, std::vector<int>, std::map<std::string, std::vector<int>>> some_instance_name;
The number of arguments can be zero, so If the variadic template should only allow a positive number of arguments, then this definition can be used: template<typename First, typename... Rest> class tuple; // takes one or more arguments
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 function called with printf-like syntax to process non-trivial objects. template<typename... Params> void my_printf(const std::string &str_format, Params... parameters);
The ellipsis (...) operator has two roles. When it occurs to the left of the name of a parameter, it declares a parameter pack. Using the parameter pack, the 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 ellipsis 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 // base case
void my_printf(const char *s)
{
while (*s)
{
if (*s == '%')
{
if (*(s + 1) == '%')
++s;
else
throw std::runtime_error("invalid format string: missing arguments");
}
std::cout << *s++;
}
}
// recursive
template<typename T, typename... Args>
void my_printf(const char *s, T value, Args... args)
{
while (*s)
{
if (*s == '%')
{
if (*(s + 1) != '%')
{
// pretend to parse the format: only works on 2-character format strings ( %d, %f, etc ); fails with %5.4f
s += 2;
// print the value
std::cout << value;
// called even when *s is 0 but does nothing in that case (and ignores extra arguments)
my_printf(s, args...);
return;
}
++s;
}
std::cout << *s++;
}
}
This is a recursive template. Notice that the variadic template version of There is no simple mechanism to iterate over the values of the variadic template. However, there are several ways to translate the argument pack into a single argument that can be evaluated separately for each parameter. Usually this will rely on function overloading, or — if the function can simply pick one argument at a time — using a dumb expansion marker: template<typename... Args> inline void pass(Args&&...) {}
which can be used as follows: template<typename... Args> inline void expand(Args&&... args)
{
pass(some_function(args)...);
}
expand(42, "answer", true);
which will expand to something like: pass(some_function(arg1), some_function(arg2), some_function(arg3) /* etc... */ );
The use of this "pass" function is necessary, since the expansion of the argument pack proceeds by separating the function call arguments by commas, which are not equivalent to the comma operator. Therefore, struct pass
{
template<typename ...T> pass(T...) {}
};
pass{(some_function(args), 1)...};
Instead of executing a function, a lambda expression may be specified and executed in place, which allows executing arbitrary sequences of statements in-place. pass{([&](){ std::cout << args << std::endl; }(), 1)...}; However, in this particular example, a lambda function is not necessary. A more ordinary expression can be used instead: pass{(std::cout << args << std::endl, 1)...}; Another way is to use overloading with "termination versions" of functions. This 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, whereas the other receives neither. (If both had the same list of initial parameters, the call would be ambiguous — a variadic parameter pack alone cannot disambiguate a call.) For example: void func() {} // termination version
template<typename Arg1, typename... Args>
void func(const Arg1& arg1, const Args&&... args)
{
process( arg1 );
func(args...); // note: arg1 does not appear here!
}
If Variadic templates can also be used in an exception specification, a base class list, or the initialization list of a constructor. For example, a class can specify the following: template <typename... BaseClasses>
class ClassName : public BaseClasses...
{
public:
ClassName (BaseClasses&&... base_classes)
: BaseClasses(base_classes)...
{}
};
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 universal references (see above), this allows for perfect forwarding: template<typename TypeToConstruct>
struct SharedPtrAllocator
{
template<typename ...Args>
std::shared_ptr<TypeToConstruct> construct_with_shared_ptr(Args&&... params)
{
return std::shared_ptr<TypeToConstruct>(new TypeToConstruct(std::forward<Args>(params)...));
}
};
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: template<typename ...Args>
struct SomeStruct
{
static const int size = sizeof...(Args);
};
The expression DDefinitionThe definition of variadic templates in D is similar to their C++ counterpart: template VariadicTemplate(Args...) { /* Body */ }
Likewise, any argument can precede the argument list: template VariadicTemplate(T, string value, alias symbol, Args...) { /* Body */ }
Basic usageVariadic arguments are very similar to constant array in their usage. They can be iterated upon, accessed by an index, have a Anything which is known at compile time can be passed as a variadic arguments. It makes variadic arguments similar to template alias arguments, but more powerful, as they also accept basic types (char, short, int...). Here is an example that prints the string representation of the variadic parameters. static int s_int;
struct Dummy {}
void main()
{
pragma(msg, StringOf!("Hello world", uint, Dummy, 42, s_int));
pragma(msg, StringOf2!("Hello world", uint, Dummy, 42, s_int));
}
template StringOf(Args...)
{
enum StringOf = Args[0].stringof ~ StringOf!(Args[1..$]);
}
template StringOf()
{
enum StringOf = "";
}
template StringOf2(Args...)
{
static if (Args.length == 0)
enum StringOf2 = "";
else
enum StringOf2 = Args[0].stringof ~ StringOf2!(Args[1..$]);
}
Outputs: "Hello world"uintDummy42s_int
"Hello world"uintDummy42s_int
AliasSeqVariadic templates are often used to create a sequence of aliases, named AliasSeq. The definition of an AliasSeq is actually very straightforward: alias AliasSeq(Args...) = Args;
This structure allows one to manipulate a list of variadic arguments that will auto-expand. The arguments must either be symbols or values known at compile time. This includes values, types, functions or even non-specialized templates. This allows any operation you would expect: import std.meta;
void main()
{
// Note: AliasSeq can't be modified, and an alias can't be rebound, so we'll need to define new names for our modifications.
alias numbers = AliasSeq!(1, 2, 3, 4, 5, 6);
// Slicing
alias lastHalf = numbers[$ / 2 .. $];
static assert(lastHalf == AliasSeq!(4, 5, 6));
// AliasSeq auto expansion
alias digits = AliasSeq!(0, numbers, 7, 8, 9);
static assert(digits == AliasSeq!(0, 1, 2, 3, 4, 5, 6, 7, 8, 9));
// std.meta provides templates to work with AliasSeq, such as anySatisfy, allSatisfy, staticMap, and Filter.
alias evenNumbers = Filter!(isEven, digits);
static assert(evenNumbers == AliasSeq!(0, 2, 4, 6, 8));
}
template isEven(int number)
{
enum isEven = (0 == (number % 2));
}
See alsoFor articles on variadic constructs other than templates References
External links |