![| Learn C++ Learn What Is Parameter Pack In C++](https://i0.wp.com/learncplusplus.org/wp-content/uploads/2023/02/pexels-edmond-dantes-4339951-scaled.jpg?resize=1140%2C694&ssl=1)
![| Learn C++ Learn What Is Parameter Pack In C++](https://i0.wp.com/learncplusplus.org/wp-content/uploads/2023/02/pexels-edmond-dantes-4339951-scaled.jpg?resize=1140%2C694&ssl=1)
One of the great features of modern C++ is the ability to define templates. A template is a simple and very powerful statement in C++ that defines the operations of a class or function. In this article, we will explain what we mean by parameter pack in C++. You can download C++ here and use a professional C++ IDE and compiler to try things out for yourself.
Table of Contents
What is parameter pack in C++?
The parameter pack symbol can be used as a function parameter pack by using ...
(3 dots) also known as the ellipsis symbol. It allows us to declare a function or template with an argument list with an unspecified number of arguments. Those arguments are then accessed in the code using ‘iterators’ to find the first argument in the list, subsequent arguments, and the last argument in the list.
Here is a simple syntax how you can use ...
in a function definition.
1 2 3 4 5 |
type function_name (type <count_parameter>, ...) { } |
Is there a simple example about parameter pack in C++?
In an example, by using parameter pack symbol, we can declare a method or function that has number of parameters and then you can list parameters, as follows,
1 2 3 4 5 |
void my_func(int count, ...) { } |
this allows you to declare an indefinite number of parameters and then you can list parameters as in the example below.
1 2 3 |
my_func(4, 450, 240, 120, 90); |
Is there a full example about parameter pack in C++?
In modern C++, we can use va_list
to declare a variable list that lists variadic function parameters. We can use va_start()
va_end()
to enable access to these arguments. Between them, we can use va_arg()
method to access the function arguments.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
#include <iostream> void my_func(int count, ...) { va_list args; va_start(args, count); for (int i = 0; i < count; i++) { std::cout << va_arg(args, int) << std::endl; } va_end(args); } int main() { my_func(4, 450, 240, 120, 90); // we have 4 parameters that comes after the 4 system("pause"); return 0; } |
How to use parameter pack in modern C++?
Sometimes, for example, when we use a template, we may need to use more template arguments. A template parameter pack is a template parameter with … (3 dots) ellipsis symbol that can be used with more template arguments (non-types, types, or templates) or it can be zero.
Here are 3 different template examples which are called as variadic class templates that use parameter pack feature in template parameters.
1 2 3 |
template < typename ... pack_name(optional)> class class_name; |
1 2 3 |
template < typename... pack_name(optional)> class class_name; |
1 2 3 |
template < typename ...pack_name(optional)> class class_name; |
As you see you can use the ellipses symbol (…). You can optionally add whitespace to both side or one side of the ellipsis – the compiler ignores the whitespaces and treats the declarations the same.
If a template has at least one parameter pack, this template is called as variadic template.
Here is the syntax for a type template parameter pack.
1 2 3 |
template < type_parameter_key ... name(optional) > declaration |
A type parameter can be one of the following types.
- typename
- class
- concept (since C++20)
For example, we can declare a template with parameter pack as shown below.
1 2 3 4 5 |
template<class ... T> class my_temp { }; |
The name is optional so if you don’t need a class name in your template you can use it with parameter pack like so:
1 2 3 4 5 |
template<class ...> class my_temp { }; |
![CBuilder11512x5121x | Learn C++ Learn What Is A Parameter Pack In C++ the C++ Builder Logo](https://i0.wp.com/learncplusplus.org/wp-content/uploads/2021/09/CBuilder_11_512x512@1x.png?resize=128%2C128)
![CBuilder11512x5121x | Learn C++ Learn What Is A Parameter Pack In C++ the C++ Builder Logo](https://i0.wp.com/learncplusplus.org/wp-content/uploads/2021/09/CBuilder_11_512x512@1x.png?resize=128%2C128)
C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.
There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise versions of C++ Builder and there is a trial version you can download from here
Design. Code. Compile. Deploy.
Start Free Trial
Free C++Builder Community Edition