Which functional feature can we expect with C++17, and for which functional feature can we hope with C++20? This is precisely the question I will concisely answer in this post.
With C++17 we get fold expressions and the new container std::optional. Even more thrilling becomes C++20. Concepts, the ranges library, and improved futures support new concepts in C++.
At first, to the near future. I will start in the next post with my systematic description of functional programming in C++. Therefore, I will be concise in this post. This post is only intended to make you more appetite.
C++11 has Variadic Templates. These are templates that can get an arbitrary number of arguments. The arbitrary number is bound in a parameter pack. New is with C++17 that you can directly reduce a parameter pack with a binary operator. Therefore, you can directly implement Haskell’s function family foldl, foldr, foldl1, and foldr1, which reduce a list to a value.
The function templates all, any, and none return at compile-time true, or false. I will have a closer look at the function template any in lines 5 and 6. The parameter pack (…) is unpacked on the binary operator (… && args). The three dots define (ellipse) the parameter pack.
For the program’s output, I use the online compiler on cppreference.com.
Haskell has the maybe monad, and C++17 will get std::optional.
std::optional stands for a calculation that maybe have a value. A find algorithm or a query of a hash table must deal with the fact that no value is available. Often, you use special values to indicate that you get no result. In short, a non-results. Often null pointers, empty strings, or special integer values are used for non-results. This is inconvenient and error-prone because you have to deal especially with non-result, and you have to distinguish the non-result from a regular result. In case of a non-result, you get with std::optional no value.
std::optional is currently in the namespace experimental. That will change with C++17. If it exists, the function getFirst will return the first element (line 8). If not, a std::optional<int> object. I use in the main function two vectors. The calls getFirst in lines 17 and 27 return a std::optional object. In the case of myInt (line 19), the object has a value; in the case of myEmptyInt (line 29), no value. Now, I can display the value of myInt (lines 20 – 22). The method value_or in lines 22 and 30 will return a value if std::optional-object has a value if not a default value.
The impact of functional programming in C++ – particularly Haskell – increases significantly with C++20. Of course, it’s a little bit risky to predict C++20. I made a mistake once and said that the following features are part of C++17, which is, of course, not true.
Promised, the details of concepts, the ranges library, and the improved futures will follow in future posts.
Type classes in Haskell are interfaces for similar types. If a type is a member of a type class, it will have specific properties. Type classes play in generic programming a similar role as interfaces in object-oriented programming. Inspired by type classes, you can specify requirements for the template parameters. The new functionality has the name concepts. For example, the sort algorithm requires that a template argument be sorted.
What are the benefits of concepts? At first, the template declaration states which properties must hold for the template arguments. Therefore, the compiler can detect the break of the contract and display an unambiguous error message.
The C++ community waits at least as eagerly for concepts as for the new ranges library from Eric Niebler.
From the birds-eye perspective, the ranges library empowers you to apply the Standard Template Library algorithms directly on the whole container. But under the hood, we get new programming techniques.
- Lazy evaluation that enables you to apply the algorithm on infinite data streams.
- Thanks to the pipe symbol, we get function composition.
- Thanks to Range comprehension, you can directly create ranges similar to list comprehension in Python or Haskell.
oddNumbers holds, as a result, the square of all odd numbers smaller than 1000: 1, 9, 25, …, 841, 961. How does it work? The function composition calculates at first to each number is square (line 1), removes all even numbers (line 2), and stops if the square of numbers is greater than 1000 (line 3). I use odds in line number 4. view::int(1) creates the infinite input stream of integers starting with 0. Odds will stop the input stream.
There is proposal n3721 for the improvement of futures. The main issue with C++11 futures is that you can not compose them. C++20 cleans it up.
The code snippet gives you an idea how the future of std::future will look like.
f1.then in line 2 returns a new future that will be executed if f1 is done with its work. any_f (line 6) will be performed if one of the futures in lines 4 and 5 is done. all_f will be performed if all of the futures in lines 8 and 9 are done.
One question is still not answered in my post. What have futures with functional programming in common? A lot! The improved futures are a monad. Suppose you don’t believe me. Watch: I see a Monad in your Future (Bartosz Milewski).
Now, it’s time to start with my systematic. In the next post, I will answer the question: What is functional programming?
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,