foldingExpressions

Fold Expressions

With fold expressions, you can implement Haskell functions foldl, foldr, foldl1, and foldr1 directly in C++. These four functions successively reduce a list to a single value.

 

Fold expressions

C++11 supports variadic templates. These are templates that can accept an arbitrary number of template arguments. A parameter pack holds an arbitrary number. Additionally, with C++17, you can directly reduce a parameter pack with a binary operator. Therefore, you can implement Haskell functions foldl, foldr, foldl1, and foldr1 in C++. Let’s look at how you can reduce a list to a value.

 

 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
// foldExpression.cpp

#include <iostream>

bool allVar(){
  return true;
}

template<typename T, typename ...Ts>
bool allVar(T t, Ts ... ts){
  return t && allVar(ts...);
}

template<typename... Args>
bool all(Args... args) { return (... && args); }

int main(){

  std::cout << std::boolalpha;

  std::cout << "allVar(): " << allVar() << std::endl;
  std::cout << "all(): " << all() << std::endl;

  std::cout << "allVar(true): " << allVar(true) << std::endl;
  std::cout << "all(true): " << all(true) << std::endl;

  std::cout << "allVar(true, true, true, false): " << allVar(true, true, true, false) << std::endl;
  std::cout << "all(true, true, true, false): " << all(true, true, true, false) << std::endl;

}

 

Both function 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 of the empty parameter pack. The recursion takes place in the function template allVar in line 9. The parameter pack is defined thanks to the three dots – a so-called ellipsis. Parameter packs support two operations. You can pack and unpack them. It is packed in line 9; unpacked in lines 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 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.

foldingExpressions

 

Rainer D 6 P2 500x500Modernes C++ Mentoring

Be part of my mentoring programs:

  • "Fundamentals for C++ Professionals" (open)
  • "Design Patterns and Architectural Patterns with C++" (open)
  • "C++20: Get the Details" (open)
  • "Concurrency with Modern C++" (starts March 2024)
  • Do you want to stay informed: Subscribe.

     

    Two variations

    Now to the two variations of fold expression that result in four different forms of fold expressions. At first, fold expression can

    1. have a default value. That value depends on the binary operator.
    2. 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:

     BinaryOperatorDefault

    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. 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 fold expressions with initial value using the same binary operator op.

    foldExpressions

    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 the once more algorithm all—this time, I use true as the initial value.

     

    template<typename... Args>
    bool all(Args... args) { return (true && ... && args); }
    

    What’s next?

    While fold expressions C++ supports the probably most genuine functional algorithm in C++17, the ranges library 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, 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, Stephen Kelley, Kyle Dean, Tusar Palauri, 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, Marco Parri Empoli, moon, Philipp Lenk, Hobsbawm, and Charles-Jianye Chen.

    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
    My special thanks to SHAVEDYAKS

    Seminars

    I’m happy to give online seminars or face-to-face seminars worldwide. Please call me if you have any questions.

    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++
    • C++20

    Online Seminars (German)

    Contact Me

    Modernes C++ Mentoring,

     

     

    0 replies

    Leave a Reply

    Want to join the discussion?
    Feel free to contribute!

    Leave a Reply

    Your email address will not be published. Required fields are marked *