With fold expressions, you can implement Haskell known functions foldl, foldr, foldl1 and foldr1 directly in C++. These four functions successively reduce a list to a single value.
C++11 supports variadic templates. These are templates that can accept an arbitrary number of template arguments. The arbitrary number is held by a parameter pack. Additionally, with C++17 we get that you can directly reduce a parameter pack with a binary operator. Therefore, you can implement Haskell known functions foldl, foldr, foldl1 and foldr1 in C++. Let's have a look, how you can reduce a list to a value.
Both functions templates allVar and all will return at compile time if all arguments are true. allVar uses variadic templates; all variadic templates in combination with fold expressions. At first to allVar. Variadic templates use recursion to evaluate their arguments. Therefore, the function allVar in line 5 is the boundary condition if the parameter pack is empty. The recursion takes place in the function template allVar in line 9. Thanks to the three dots - a so-called ellipsis -, the parameter pack are defined. Parameter packs support two operations. You can pack and unpack them. It is packed in line 9; unpacked in line 10 and 11. Line 11 needs our full attention. Here, the head of the parameter pack t is combined with the rest ts of the parameter pack ts by 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 make our job easier. With fold expressions, you can directly reduce the parameter pack with the help of the binary operator.
Here is the output of the program.
Now to the two variations of fold expression that result in four different forms of fold expressions. At first, fold expression can
- have a default value. That value depends on the binary operator.
- be reduced from the left of the 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:
For binary operators that have no default value, you have to provide an initial value. For binary operators that have a default value, you can specify an initial value.
If the ellipsis stands left of the parameter pack, the parameter pack will be processed from the left. The same holds for right. This is also true if you provide an initial value.
The following table shows the four variations 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 its list at run time.
The small code snippet shows once more the algorithm all. This time I use true as the initial value.
While fold expressions C++ supports the probably most genuine functional algorithm in C++17, the ranges library in the contrary extends C++20 with three powerful functional concepts. Therefore, the next post will be about the ranges library from Eric Niebler that gives lazy evaluation, function composition and range comprehension a home in functional C++.
Thanks a lot to my Patreon Supporters: Matt Braun, Roman Postanciuc, Tobias Zindl, Marko, G Prvulovic, Reinhold Dröge, Abernitzke, Frank Grimm, Sakib, Broeserl, António Pina, Sergey Agafyin, Андрей Бурмистров, Jake, GS, Lawton Shoemake, Animus24, Jozo Leko, John Breland, espkk, Louis St-Amour, Venkat Nandam, Jose Francisco, Douglas Tinkham, Kuchlong Kuchlong, Robert Blanch, Truels Wissneth, Kris Kafka, Mario Luoni, Neil Wang, Friedrich Huber, lennonli, Pramod Tikare Muralidhara, Peter Ware, Tobi Heideman, Daniel Hufschläger, Red Trip, Alexander Schwarz, Tornike Porchxidze, Alessandro Pezzato, Evangelos Denaxas, Bob Perry, Satish Vangipuram, Andi Ireland, Richard Ohnemus, Satish Vangipuram, and Michael Dunsky.
My special thanks to Embarcadero
I'm happy to give online seminars or face-to-face seminars worldwide. Please call me if you have any questions.
- C++20: 10.08.2021 - 12.08.2021 (Termingarantie)
- Embedded Programmierung mit modernem C++: 21.09.2021 - 23.09.2021
- Clean Code: Best Practices für modernes C++: 14.12.2021 - 16.12.2021
- Concurrency mit modernem C++ 08.02.2022 - 09.02.2022
- Design Pattern und Architekturpattern mit C++ 05.04.2022 - 07.04.2022
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 Patterns and Architecture Patterns with C++
- Embedded Programming with Modern C++
- Generic Programming (Templates) with C++
- Phone: +49 152 31965939
- German Seminar Page: www.ModernesCpp.de
- English Seminar Page: www.ModernesCpp.net