C++C++11C++14C++17Learn C++Templates

Learn What Is A Parameter Pack In C++

Learn What Is Parameter Pack In C++

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. 

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.

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,

this allows you to declare an indefinite number of parameters and then you can list parameters as in the example below.

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.

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.

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.

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.

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:

Learn What Is A Parameter Pack In C++ - the C++ Builder Logo

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

close

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.

About author

Dr. Yilmaz Yoru has 33+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He was born in 1974, Eskisehir-Turkey, started coding in college and graduated from the department of Mechanical Engineering of Eskisehir Osmangazi University in 1997. He worked as a research assistant at the same university for more than 10 years. He received his MSc and PhD degrees from the same department at the same university. Since 2012, he is the founder and CEO of Esenja LLC Company. He has married and he is a father of a son. Some of his interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14C++17Introduction to C++Learn C++Syntax

What Is Static Assertion And How To Use static_assert In C++?

C++C++11C++14C++17Introduction to C++Learn C++

How To Learn initializer_list (std::initializer_list) In Modern C++

C++C++11C++14C++17C++20Introduction to C++Learn C++Syntax

Learn How To Use enum In Unscoped Enumeration In C++

C++C++11C++14C++17C++20Introduction to C++Learn C++Syntax

Everything You Need To Know About Strongly Typed Enums In Modern C++