Commit 3de873ab authored by ROUVREAU Vincent's avatar ROUVREAU Vincent Committed by GILLES Sebastien
Browse files

typo, and adding text about functor arguments

parent 688329e1
%% 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<span class="tocSkip"></span></h1>
<div class="toc"><ul class="toc-item"><li><span><a href="#What-is-a-functor?" data-toc-modified-id="What-is-a-functor?-1">What is a functor?</a></span></li><li><span><a href="#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
// definition is used for operators
}
private :
int constant_ = 0.;
} ;
```
%% Cell type:code id: tags:
``` C++17
LinearFunction::LinearFunction(int constant)
: constant_(constant)
{ }
```
%% Cell type:code id: tags:
``` C++17
#include <iostream>
{
int values[10] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
LinearFunction Double(2);
LinearFunction Triple(3);
for (double value : values)
std::cout << value << "\t" << Double(value) << "\t" << Triple(value) << std::endl;
}
```
%% Cell type:markdown id: tags:
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`:
%% Cell type:code id: tags:
``` C++17
#include <algorithm>
#include <iostream>
#include <vector>
{
std::vector<int> values { -980, 12, 2987, -8323, 4, 275, -8936, 27, -988, 92784 };
std::sort(values.begin(), values.end(), std::greater<int>());
for (auto value : values)
std::cout << value << " ";
}
```
%% Cell type:markdown id: tags:
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:
© _CNRS 2016_ - _Inria 2018-2021_
_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)_
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment