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.
Table of Contents
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 operatorfold_op
is a binary operator, one of the + – * / % ^ & | ~ = < > << >> += -= *= /= %= ^= &= |= <<= >>= == != <= >= && || , .* ->* operatorsfold_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),
1 2 3 |
( fold_pack fold_op ... ) |
here is an unary right fold expression example,
1 2 3 4 5 6 |
template <typename ... Args> bool URF(Args ... args) { return (args && ...); // Unary Right Fold } |
Is there a simple example about unary left fold expression?
Unary left fold expression syntax (since C++17).
1 2 3 |
( ... fold_op fold_pack ) |
here is an unary left fold expression example,
1 2 3 4 5 6 |
template <typename ... Args> bool ULF(Args ... args) { return (... && args); // Unary Left Fold } |
Is there a simple example about binary right fold expression?
Binary right fold expression syntax (since C++17).
1 2 3 |
( fold_pack fold_op ... fold_op fold_init ) |
here is a binary right fold expression example,
1 2 3 4 5 6 |
template<typename... Args> int BRF(Args&&... args) { return (args + ... + 100); // Binary right fold addition } |
Is there a simple example about binary left fold expression?
Binary left fold expression syntax (since C++17).
1 2 3 |
( fold_init fold_op ... fold_op fold_pack ) |
here is a binary left fold expression example,
1 2 3 4 5 6 |
template<typename... Args> int BLF(Args&&... args) { return (1 * ... * args); // Binary left fold multiplication } |
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.
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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
#include <iostream> template <typename ... Args> bool URF(Args ... args) { return (args && ...); // Unary Right Fold } template <typename ... Args> bool ULF(Args ... args) { return (... && args); // Unary Left Fold } template<typename... Args> int BRF(Args&&... args) { return (args + ...+ 100); // Binary Right Fold Addition } template<typename... Args> int BLF(Args&&... args) { return (1 *...* args); // Binary left Fold Multiplication } int main() { bool urf = URF(true, true, true, false); std::cout << "Unary Right Fold: " << std::boolalpha << urf << std::endl; bool ulf = ULF(true, true, true, false); std::cout << "Unary Left Fold: " << std::boolalpha << ulf << std::endl; auto brf = BRF(1, 2, 3, 4, 5); std::cout << "Binary Right Fold: " << brf << std::endl; auto blf = BLF(1, 2, 3, 4, 5); std::cout << "Binary Left Fold: " << blf << std::endl; system("pause"); return 0; } |
and the output will be as follows:
1 2 3 4 5 6 |
Unary Right Fold: false Unary Left Fold: false Binary Right Fold: 115 Binary Left Fold: 120 |
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.
Design. Code. Compile. Deploy.
Start Free Trial
Free C++Builder Community Edition