In my last two posts, “Variadic Templates or the Power of Three Dots” and “More about Variadic Templates“, I introduced variadic templates. This post goes one step further in the future, and presents fold expressions that can directly reduce a parameter pack with a binary operator.
I intentionally skipped one import use case for variadic templates: the perfect factory function. When you combine perfect forwarding with variadic templates, you get the perfect factory function. The perfect factory function is a function that can accept an arbitrary number of arguments of any value category (lvalue or rvalue). Curious? Read my previous post, “C++ Core Guidelines: Rules for Variadic Templates“.
But now, I want to present something new with fold expressions.
C++11 supports variadic templates. These are templates that can accept an arbitrary number of template parameters. A so-called parameter pack holds the arbitrary number. Additionally, with C++17, we got fold expressions. Thanks to fold expressions, you can directly reduce a parameter pack with a binary operator. Let’s look at reducing an arbitrary number of numbers to a value.
Both function templates allVar and all return at compile time if all arguments are true. allVar uses variadic templates; all fold expressions.
The variadic templates
allVar applies recursion to evaluate its arguments. The function allVar (1) is the boundary condition if the parameter pack is empty. The recursion takes place in the function template allVar (2). The three dots stand for the so-called parameter pack. Parameter packs support two operations. You can pack or unpack them. It is packed in line (2); unpacked in (3) and (4).
Line (4) requires our full attention. Here, the head of the parameter pack t is combined with the rest ts of the parameter pack ts using the binary operator &&. The call allVar(ts …) triggers the recursion. The call includes a parameter pack that is the original one reduced by the head. Fold expressions (5) make our job easier. With fold expressions, you can directly reduce the parameter pack with the help of the binary operator:
(... && args).
Here is the output of the program.
A fold expression applies a binary operator to a parameter pack.
Application of the binary Operator
A fold expression can apply the binary operator in two different ways.
- With and without an initial value.
- Fold the variadic pack from left or from right
There is a subtle difference between the algorithm allVar and all. All have the default value
true for the empty parameter pack.
C++17 supports 32 binary operators in fold expressions: “+ – * / % ^ & | = < > << >> += -= *= /= %= ^= &= |= <<= >>= == != <= >= && || , .* ->*” . A few of them have default-values:
You have to provide an initial value for binary operators with no default value. You can specify an initial value for binary operators with a default value.
If the ellipsis stands left of the parameter pack, the parameter pack will be processed from the left. If right of the parameter pack, it will be processed from the right. This is also true if you provide an initial value.
Fold expression allows it to implement Haskell functions foldl, foldr, foldl1, and foldr1 direct in C++.
A Flavor of Haskell
The following table shows the four variations of fold expression and their Haskell pendants. The C++17 standard requires that fold expressions with initial value use the same binary operator op.
The C++ and Haskell variations differ in two points. The C++ version uses the default value as the initial value; the Haskell version uses the first element as the initial value. The C++ version processes the parameter pack at compile time, and the Haskell version processes its list at run time.
The following program shows all four variations of fold expression. Each one subtracts a few numbers.
diffL1 (1) and
diffL (3) process the numbers from the left and the functions
diffR1 (2) and
diffR (3) process them from the right. Additionally, the functions
diffR use an initial start value. The comments in the
main function shows the processing steps.
I execute the Haskell pendant directly in the Haskell shell.
Variadic templates, particularly fold expressions, enable it to write concise expressions for repeated operations. Learn more about it in my next post.
Thanks a lot to my Patreon Supporters: Matt Braun, Roman Postanciuc, Tobias Zindl, G Prvulovic, Reinhold Dröge, Abernitzke, Frank Grimm, Sakib, Broeserl, António Pina, Sergey Agafyin, Андрей Бурмистров, Jake, GS, Lawton Shoemake, Jozo Leko, John Breland, Venkat Nandam, Jose Francisco, Douglas Tinkham, Kuchlong Kuchlong, Robert Blanch, Truels Wissneth, Kris Kafka, Mario Luoni, Friedrich Huber, lennonli, Pramod Tikare Muralidhara, Peter Ware, Daniel Hufschläger, Alessandro Pezzato, Bob Perry, Satish Vangipuram, Andi Ireland, Richard Ohnemus, Michael Dunsky, Leo Goodstadt, John Wiederhirn, Yacob Cohen-Arazi, Florian Tischler, Robin Furness, Michael Young, Holger Detering, Bernd Mühlhaus, Matthieu Bolt, Stephen Kelley, Kyle Dean, Tusar Palauri, Dmitry Farberov, Juan Dent, George Liao, Daniel Ceperley, Jon T Hess, Stephen Totten, Wolfgang Fütterer, Matthias Grün, Phillip Diekmann, Ben Atakora, Ann Shatoff, Rob North, Bhavith C Achar, and Marco Parri Empoli.
Thanks, in particular, to Jon Hess, Lakshman, Christian Wittenhorst, Sherhy Pyton, Dendi Suhubdy, Sudhakar Belagurusamy, Richard Sargeant, Rusty Fleming, John Nebel, Mipko, Alicja Kaminska, Slavko Radman, and David Poole.
|My special thanks to Embarcadero|
|My special thanks to PVS-Studio|
|My special thanks to Tipi.build|
|My special thanks to Take Up Code|
I’m happy to give online seminars or face-to-face seminars worldwide. Please call me if you have any questions.
- Embedded Programmierung mit modernem C++ 12.12.2023 – 14.12.2023 (Präsenzschulung, Termingarantie)
Standard Seminars (English/German)
Here is a compilation of my standard seminars. These seminars are only meant to give you a first orientation.
- C++ – The Core Language
- C++ – The Standard Library
- C++ – Compact
- C++11 and C++14
- Concurrency with Modern C++
- Design Pattern and Architectural Pattern with C++
- Embedded Programming with Modern C++
- Generic Programming (Templates) with C++
- Clean Code with Modern C++
- Phone: +49 7472 917441
- Mobil:: +49 176 5506 5086
- Mail: schulung@ModernesCpp.de
- German Seminar Page: www.ModernesCpp.de
- Mentoring Page: www.ModernesCpp.org
Modernes C++ Mentoring,