Namespaces
Variants
Views
Actions

Parameter pack

From cppreference.com
< cpp‎ | language
Revision as of 05:50, 5 March 2013 by Cubbi (Talk | contribs)

 
 
C++ language
General topics
Flow control
Conditional execution statements
Iteration statements (loops)
Jump statements
Functions
Function declaration
Lambda function declaration
inline specifier
Exception specifications (deprecated)
noexcept specifier (C++11)
Exceptions
Namespaces
Types
Specifiers
decltype (C++11)
auto (C++11)
alignas (C++11)
Storage duration specifiers
Initialization
Expressions
Alternative representations
Literals
Boolean - Integer - Floating-point
Character - String - nullptr (C++11)
User-defined (C++11)
Utilities
Attributes (C++11)
Types
typedef declaration
Type alias declaration (C++11)
Casts
Implicit conversions - Explicit conversions
static_cast - dynamic_cast
const_cast - reinterpret_cast
Memory allocation
Classes
Class-specific function properties
Special member functions
Templates
Template specialization
Parameter packs (C++11)
Miscellaneous
 

A template parameter pack is a template parameter that accepts zero or more template arguments (non-types, types, or templates). A function parameter pack is a function parameter that accepts zero or more function arguments.

A template with at least one parameter pack is called a variadic template.

Contents

Syntax

Template parameter pack (appears in a class template and in a function template parameter list)

Template:sparam ... Template:sparam (1) (since C++11)
typename|class ... Template:sparam (2) (since C++11)
template < Template:sparam > ... Template:sparam (3) (since C++11)

Function parameter pack (appears in a function parameter list)

Template:sparam ... Template:sparam (4) (since C++11)

Parameter pack expansion (appears in a body of a variadic function template)

Template:sparam ... (5) (since C++11)

Explanation

1) A non-type template parameter pack with an optional name
2) A type template parameter pack with an optional name
3) A template template parameter pack with an optional name
4) A function parameter pack
5) Parameter pack expansion: expands to comma-separated list of zero or more patterns. Pattern must include at least one parameter pack.

Example

#include <iostream>
 
void tprintf(const char* format) // base function
{
    std::cout << format;
}
 
template<typename T, typename... Targs>
void tprintf(const char* format, T value, Targs... Fargs) // recursive variadic function
{
    for ( ; *format != '\0'; format++ ) {
        if ( *format == '%' ) {
           std::cout << value;
           tprintf(format+1, Fargs...); // recursive call
           return;
        }
        std::cout << *format;
    }
}
 
int main()
{
    tprintf("% world% %\n","Hello",'!',123);
    return 0;
}

Output:

Hello world! 123

The above example defines a function similar to std::printf, that replace each occurrence of the character % in the format string with a value.

The first overload is called when only the format string is passed and there is no parameter expansion.

The second overload contains a separate template parameter for the head of the arguments and a parameter pack, this allows the recursive call to pass only the tail of the parameters until it becomes empty.

Targs is the template parameter pack and Fargs is the function parameter pack

See also

function template
class template
sizeof... Queries the number of elements in a parameter pack.
C-style variadic functions
Preprocessor macros Can be variadic as well