Site icon Learn C++

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,

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),

[crayon-6646f8e8c37ee493061002/]

here is an unary right fold expression example,

[crayon-6646f8e8c37f4215629685/]

Is there a simple example about unary left fold expression?

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

[crayon-6646f8e8c37f5315109677/]

here is an unary left fold expression example,

[crayon-6646f8e8c37f7597794202/]

Is there a simple example about binary right fold expression?

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

[crayon-6646f8e8c37f9027990677/]

here is a binary right fold expression example,

[crayon-6646f8e8c37fa192920265/]

Is there a simple example about binary left fold expression?

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

[crayon-6646f8e8c37fc994232134/]

here is a binary left fold expression example,

[crayon-6646f8e8c37fe767087067/]

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.

[crayon-6646f8e8c37ff761968172/]

and the output will be as follows:

[crayon-6646f8e8c3801071834361/]

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,

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.

Exit mobile version