C++C++17C++20Learn C++SyntaxTemplates

What Are The New Fold Expressions In C++ 17

What Are The New Fold Expressions In C++ 17

C++17 is another big milestone in the history of C++, it comes with a lot of new features. In C++17, the fold expressions feature is a powerful feature that allows us to fold a parameter pack over a binary operator. Folding Expressions are very useful with variadic templates, and this feature makes template arguments more readable and concise. There are 4 different types of usage and in this post we will give syntax and simple examples about each of them.

What are the fold expressions that comes in C++ 17 features?

The fold expressions are established after the C++17 standard, they are used to fold (reduce) a parameter pack (fold_pack) over a binary operator (fold_op). The opening and closing parentheses are required in a fold expression.

In a folding expression there are 4 parameters,

  • fold_pack is an expression that has parameter pack and no operator
  • fold_op is a binary operator, one of the + – * / % ^ & | ~ = < > << >> += -= *= /= %= ^= &= |= <<= >>= == != <= >= && || , .* ->* operators
  • fold_init is an initial expression at the beginning or at the end
  • ... is an ellipses symbol that used for arguments

There are 4 different syntax in usage, now let’s see them in examples,

Is there a simple example about unary right fold expression?

Unary right fold expression syntax (since C++17),

here is an unary right fold expression example,

Is there a simple example about unary left fold expression?

Unary left fold expression syntax (since C++17).

here is an unary left fold expression example,

Is there a simple example about binary right fold expression?

Binary right fold expression syntax (since C++17).

here is a binary right fold expression example,

Is there a simple example about binary left fold expression?

Binary left fold expression syntax (since C++17).

here is a binary left fold expression example,

Is there a full example about fold expressions in C++ 17?

Here is a full example about fold expressions in C++17 that has 4 different types in usage.

and the output will be as follows:

For more details, please see this https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4295.html
and this https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/p0036r0.pdf

If you want to know more about about Variadic Templates, here are examples,

What Are The New Fold Expressions In C++ 17 C++ Builder logo

C++ Builder is the easiest and fastest C and C++ compiler and IDE for building simple or professional applications on the Windows operating system. 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 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.

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.


Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial

Free C++Builder Community Edition

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14C++17C++20Introduction to C++Learn C++

Learn Copy Constructors in C++ Classes

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

Learn How To Use Types Of Destructors In C++?

C++C++11C++14Learn C++Syntax

How To Convert u32string To A wstring In C++

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

How To Learn The Move Constructors In Modern C++?