"We can provide a class with an operator `operator()` that allows to give its objects a function behavior. In other words: if you write an expression in which an object is used as if it were a function, it is its execution operator that is invoked. \n",
"We can provide a class with an `operator()` that allows to give its objects a function behavior. In other words: if you write an expression in which an object is used as if it were a function, it is its execution operator that is invoked. \n",
"\n",
"We can also see these *functional objects*, or *functors*, as functions to which we would have added state parameters from one call to another.\n",
"\n",
"The return type is not constrained and might be defined as you wish in the class (`double` is used here):"
"The return type and the arguments are not constrained and might be defined as you wish in the class (`int` is used here for return type, and an `int` is required as argument):"
]
},
{
...
...
@@ -121,7 +121,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"C++ 11 and above limits the need for functors with lambda functions (see [earlier](../1-ProceduralProgramming/4-Functions.ipynb#Lambda-functions)), but in older codes functors were one of the way to pass your own rules to some STL algorithms, along with pointer to functions."
"C++ 11 and above limits the need for functors with lambda functions (see [earlier](../1-ProceduralProgramming/4-Functions.ipynb#Lambda-functions)), but in older codes, functors were one of the way to pass your own rules to some STL algorithms, along with pointer to functions."
]
},
{
...
...
%% Cell type:markdown id: tags:
# [Getting started in C++](./) - [Operators](./0-main.ipynb) - [Functors](./5-Functors.ipynb)
%% Cell type:markdown id: tags:
<h1>Table of contents<spanclass="tocSkip"></span></h1>
<divclass="toc"><ulclass="toc-item"><li><span><ahref="#What-is-a-functor?"data-toc-modified-id="What-is-a-functor?-1">What is a functor?</a></span></li><li><span><ahref="#Functors-in-STL"data-toc-modified-id="Functors-in-STL-2">Functors in STL</a></span></li></ul></div>
%% Cell type:markdown id: tags:
## What is a functor?
We can provide a class with an operator `operator()` that allows to give its objects a function behavior. In other words: if you write an expression in which an object is used as if it were a function, it is its execution operator that is invoked.
We can provide a class with an `operator()` that allows to give its objects a function behavior. In other words: if you write an expression in which an object is used as if it were a function, it is its execution operator that is invoked.
We can also see these *functional objects*, or *functors*, as functions to which we would have added state parameters from one call to another.
The return type is not constrained and might be defined as you wish in the class (`double` is used here):
The return type and the arguments are not constrained and might be defined as you wish in the class (`int` is used here for return type, and an `int` is required as argument):
%% Cell type:code id: tags:
``` C++17
class LinearFunction
{
public :
LinearFunction(int constant);
int operator()(int value)
{
return constant_ * value; // here due to usual Xeus-cling issue when out of class
This might seem inconsequential, but they are sometimes extremely useful.
## Functors in STL
STL itself defines some functors: imagine you want to sort a `std::vector` decreasingly; you can't directly put `>` as the comparison operation in `sort` and therefore may use a specific STL-defined `greater`:
C++ 11 and above limits the need for functors with lambda functions (see [earlier](../1-ProceduralProgramming/4-Functions.ipynb#Lambda-functions)), but in older codes functors were one of the way to pass your own rules to some STL algorithms, along with pointer to functions.
C++ 11 and above limits the need for functors with lambda functions (see [earlier](../1-ProceduralProgramming/4-Functions.ipynb#Lambda-functions)), but in older codes, functors were one of the way to pass your own rules to some STL algorithms, along with pointer to functions.
_This notebook is an adaptation of a lecture prepared by David Chamont (CNRS) under the terms of the licence [Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0)](http://creativecommons.org/licenses/by-nc-sa/4.0/)_
_The present version has been written by Sébastien Gilles and Vincent Rouvreau (Inria)_