One short remark to the second point. The function makeAdd returns the lambda function [](int a, int b){ return a + b; }. This function needs two int arguments and returns an int value: std::function<int(int,int)>. You can bind the return value of makeAdd to the generic function wrapper myAdd and execute it.

Function pointers are the first-class functions of the poor man. Even C has function pointers. C++ goes a few steps further. In particular, you can connect the evolution of the function concept with the evolution of C++.

The evolution of the function concept

Simplified, the evolution consists of four steps.

C knows functions. C++ added function objects. These are objects, which behave like functions because their call operator is overloaded. C++11 added lambda-functions; C++14 made them generic. Each step made functions in C++ even more powerful.

Functions => Function objects: Function object can have in contrary to functions a state. Therefore, they have a kind of memory and you can configure them.

Function objects => Lambda functions: Lambda functions are usually implemented just in the place of their usage. That improves the readability of the code, reduces your typing to its bare minimum, and gives the optimizer maximum insight into your code. Therefore, the optimizer has more opportunities to do its job.

Lambda functions => Generic lambda functions: Generic lambda functions are quite similar to function templates but are a lot easier to implement. They can be widely used because as closures they capture their environment and you can parametrize their type.

Admittedly, that was a lot of theory. Therefore, the example is following right now. Here are the four steps of the function concepts in C++.

The program begins with the function addMe (line 8 - 10), the function object AddMe (line 12 - 23), the lambda function (line 45 and 51), and the generic lambda function in line 59, 63, and 64. The purpose of the program is to concatenate in different ways the strings of the vector myString (line 27) with the help of the algorithm std::accumulate. Therefore, it is quite comfortable that std::accumulate is a higher order function (wait for my next post) and can accept first-class functions. I use a function pointer, a function object and a (generic) lambda function as the first-class function.

I apply in line 29 the function pointer addMe. The function object AddMe and AddMe(:) (line 35 and 39) are more comfortable to use because I can parametrize the glue between the strings. The lambda functions offer the same comfort (line 45 and 51). In particular, the lambda function lambdaFunc uses a copy of the variable glue (line 49). Therefore, the lambda function is a closure. The generic lambda function [glue](auto fir, auto sec){ return fir + glue + sec; }) (line 59) is the most interesting one. As closure, they also use the variable glue. I apply the generic lambda functionlambdaFuncGeneric to a std::vector<std::string> (line 27) and astd::vector<int> (line 69). In case of std::vector<int> the result is 55.

At the end, the output of the program.

The evolution will not end with C++14. In C++20 we get with high probability coroutines, which are a generalisation of functions. Coroutines can be suspended and resumed. I will write a post about it. So, stay tuned.

But, that can be done easier

For didactical reasons, I used std::accumulate to add pairs of elements together. That can be done a lot easier - even for generic lambda functions - because of std::accumulate has a simple version, which needs no callable. The simple version just adds their elements together starting with the first element. Therefore, I can write line 65 a lot simpler: auto fromLambdaFuncGeneric1= std::accumulate(myStrings.begin(), myStrings.end(), std::string{}). The same holds forfromLambdaFuncGeneric2.

What's next?

I already mentioned it in the post. The classical counterpart to first-class functions is higher-order functions because they get first-class functions. The next post will be about higher-order functions. The next property of functional programming.

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