C++0x will allow template to take an arbitrary number of arguments. What is the best use of this feature other than implementing tuples ?
Variadic templates are class or function templates, that can take any variable(zero or more) number of arguments. In C++, templates can have a fixed number of parameters only that have to be specified at the time of declaration. However, variadic templates help to overcome this issue.
With the variadic templates feature, you can define class or function templates that have any number (including zero) of parameters. To achieve this goal, this feature introduces a kind of parameter called parameter pack to represent a list of zero or more parameters for templates.
variadic (not comparable) (computing, mathematics, linguistics) Taking a variable number of arguments; especially, taking arbitrarily many arguments.
Parameter packs (C++11) A parameter pack can be a type of parameter for templates. Unlike previous parameters, which can only bind to a single argument, a parameter pack can pack multiple parameters into a single parameter by placing an ellipsis to the left of the parameter name.
Sample to 3:
template<typename... T> struct flexible : T... { flexible(): T()... { } };
Sample to 4:
struct my_container { template<typename... T> my_container(T&&... t) { } }; my_container c = { a, b, c };
Sample to 5:
template<char... digits> int operator "" b() { return convert<digits...>::value; }
See this example code: here
Maybe the talk by Andrei Alexandrescu on the Going Native 2012 event, will be of your interest:
Here is the video and Here the documentation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With