"What we pass to std::set's constructor is a **function pointer** of type\n",
"\n",
" bool (*) ( const myPair&, const myPair& )"
]
},
{
"cell_type": "markdown",
"id": "dc79e822",
"metadata": {},
"source": [
"***\n",
"- This way to work with functions is something that C++ inherited from C.\n",
"- Not only in the STL, but also in scientific programming we often have the case that we implement something, but want to leave some details to the user: \n",
" - Consider e.g. implementing an algorithm to approximate the integral\n",
"$$I = \\int_a^b f(x)\\, dx$$\n",
" - When we code that we would like to leave the details of the integrand to be specified flexibly by the user. \n",
" - As with std::set we can let the user provide a function that we then call inside our code.\n",
" - This is known as a **callback**.\n",
"- The main downside to using function (pointers) in this approach is that a free function is **stateless**. In order to influence its behaviour we have to pass arguments through its interface. That does not work nicely with the callback idea.\n",
" - Note: We can add *state* to a function by making some of its local variables ```static```. See the ```accumulate``` examples.\n",
" - However, a function is a global entity. Thus, there can always only be one state for it."
]
},
{
"cell_type": "markdown",
"id": "9ce08962",
"metadata": {},
"source": [
"#### Functors\n",
"In C++ we can as one alternative use a **function object**, a.k.a. **functor**. This is an object of a class that overloads the function call operator ```operator()```."
"Now that was a simple functor and did only demonstrate **how** it works and not **why** that approach can be advantageous.\n",
"\n",
"**Example 2:** \n",
"In our second example we will use a stateful functor that allows to perform different binary operations. The type of operation will be passed via its constructor."
]
},
{
"cell_type": "code",
"execution_count": 23,
"id": "8c0f30c5",
"metadata": {},
"outputs": [],
"source": [
"#include <iostream>\n",
"\n",
"class BinaryOperator {\n",
"\n",
"public:\n",
"\n",
" typedef enum { ADD, MULT } type;\n",
"\n",
" BinaryOperator() = delete;\n",
" BinaryOperator( BinaryOperator::type op ) : whatToDo( op ) {};\n",
"\n",
" int operator() ( int a, int b ) {\n",
" switch( whatToDo ) {\n",
" case ADD:\n",
" return a+b;\n",
" case MULT:\n",
" return a*b;\n",
" }\n",
" }\n",
"\n",
"private:\n",
" type whatToDo;\n",
"\n",
"};"
]
},
{
"cell_type": "markdown",
"id": "6adaab27",
"metadata": {},
"source": [
"**Example 3:** \n",
"So how would our set example for myPair look like with a functor?"
]
},
{
"cell_type": "code",
"execution_count": 25,
"id": "856ee747",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Writing functor.cpp\n"
]
}
],
"source": [
"%%file functor.cpp\n",
"\n",
"#include <iostream>\n",
"#include <set>\n",
"\n",
"struct myPair {\n",
" myPair( int fir, int sec ) : fir_( fir ), sec_( sec ) {\n",
What we pass to std::set's constructor is a **function pointer** of type
bool (*) ( const myPair&, const myPair& )
%% Cell type:markdown id:dc79e822 tags:
***
- This way to work with functions is something that C++ inherited from C.
- Not only in the STL, but also in scientific programming we often have the case that we implement something, but want to leave some details to the user:
- Consider e.g. implementing an algorithm to approximate the integral
$$I = \int_a^b f(x)\, dx$$
- When we code that we would like to leave the details of the integrand to be specified flexibly by the user.
- As with std::set we can let the user provide a function that we then call inside our code.
- This is known as a **callback**.
- The main downside to using function (pointers) in this approach is that a free function is **stateless**. In order to influence its behaviour we have to pass arguments through its interface. That does not work nicely with the callback idea.
- Note: We can add *state* to a function by making some of its local variables ```static```. See the ```accumulate``` examples.
- However, a function is a global entity. Thus, there can always only be one state for it.
%% Cell type:markdown id:9ce08962 tags:
#### Functors
In C++ we can as one alternative use a **function object**, a.k.a. **functor**. This is an object of a class that overloads the function call operator ```operator()```.
%% Cell type:code id:9bf146ec tags:
``` C++14
#include <iostream>
class greeter{
public:
void operator() ( const std::string& str ) {
std::cout << "Greetings " << str << std::endl;
}
};
```
%% Cell type:code id:b13aa200 tags:
``` C++14
int main() {
greeter hello;
hello( "Class" );
}
```
%% Cell type:code id:f7a3701a tags:
``` C++14
main();
```
%% Output
Greetings Class
%% Cell type:markdown id:58a187c4 tags:
***
Now that was a simple functor and did only demonstrate **how** it works and not **why** that approach can be advantageous.
**Example 2:**
In our second example we will use a stateful functor that allows to perform different binary operations. The type of operation will be passed via its constructor.
%% Cell type:code id:8c0f30c5 tags:
``` C++14
#include <iostream>
class BinaryOperator {
public:
typedef enum { ADD, MULT } type;
BinaryOperator() = delete;
BinaryOperator( BinaryOperator::type op ) : whatToDo( op ) {};
int operator() ( int a, int b ) {
switch( whatToDo ) {
case ADD:
return a+b;
case MULT:
return a*b;
}
}
private:
type whatToDo;
};
```
%% Cell type:markdown id:6adaab27 tags:
**Example 3:**
So how would our set example for myPair look like with a functor?
%% Cell type:code id:856ee747 tags:
``` C++14
%%file functor.cpp
#include <iostream>
#include <set>
struct myPair {
myPair( int fir, int sec ) : fir_( fir ), sec_( sec ) {