Posts

Showing posts with the label variadic template

C++11 What is Variadic template and How it works?

Variadic templates are a feature introduced in C++11 that allows functions and classes to take a variable number of arguments of different types. The syntax for a variadic template is to use an ellipsis ... after the type parameter. Here is an example of a variadic function that takes a variable number of arguments and returns their sum: template<typename T> T sum(T value) { return value; } template<typename T, typename... Args> T sum(T value, Args... args) { return value + sum(args...); } The first template function is the base case, which simply returns the value passed in. The second template function is the recursive case, which adds the current value to the sum of the remaining arguments. Variadic templates can also be used to create variadic classes. Here is an example of a variadic tuple class that stores a tuple of values of different types: template<typename... Ts> class Tuple {}; template<typename T, typename... Ts> class Tuple<T,...