Fold Expressions


With fold expressions you can implement the from Haskell known 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. 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 the from Haskell known functions foldl, foldr, foldl1 and foldr1 in C++. Let's have a look, how you can reduce a list to a value.


// 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 its 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 is 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.


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 has 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.


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 in 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++.













title page smalltitle page small Go to Leanpub/cpplibrary "What every professional C++ programmer should know about the C++ standard library".   Get your e-book. Support my blog.



Tags: C++17


0 #1 mehndi 2017-02-13 08:59
Great post.
0 #2 Jacki 2017-04-12 05:22
I blog quite often and I seriously thank you for your information.
This article has truly peaked my interest.
I will take a note of your website and keep
chscking for new details about once per week.
I subscribed to your Feed too.
0 #3 Kenny 2017-05-14 00:40
Loving the info on this website, you have done outstanding job on the
blog posts.
0 #4 đèn gầm 2017-05-21 02:56
Keep on working, great job!
0 #5 Christoper 2017-06-19 03:02
I blog often and I genuinely thank you for your content.

Your article has really peaked my interest. I will take a note oof your blog and keep checking for new
information about once a week. I subscribed to your Feed
as well.
0 #6 lasertest 2017-07-19 05:49
Hi, yup this post is genuinely good and I have learned lot
of things from it on the topic of blogging. thanks.
0 #7 Verna 2017-08-09 16:58
Woah! I'm really enjoying the template/theme of this site.
It's simple, yet effective. A lot of times it's very hard to get that
"perfect balance" between usability and appearance.
I must say you have done a awesome job with this. In addition, the
blog loads extremely fast for me on Firefox. Exceptional Blog!

My Newest E-Books

Course: Modern C++ Concurrency in Practice

Course: C++ Standard Library including C++14 & C++17

Course: Embedded Programming with Modern C++

Course: Generic Programming (Templates)

Course: C++ Fundamentals for Professionals

Subscribe to the newsletter (+ pdf bundle)

Blog archive

Source Code


Today 5952

Yesterday 7978

Week 30517

Month 185112

All 4806006

Currently are 208 guests and no members online

Kubik-Rubik Joomla! Extensions

Latest comments