gfvalvo:
Does it mean that if the Lambda does not capture any local variables then you can assign it to a function pointer?
Correct. Otherwise, where would it save the copies of or references to the local variables?
gfvalvo:
That's interesting. My code from Reply #6 compiles without complaint for both Uno and Teensy 3.2 using Arduino v1.8.5. That's a mystery for another day.
That's probably because I'm using a newer version of the Arduino AVR Core. They finally updated their compiler to GCC 7.
But they still compile everything using C++11, even though GCC 7 supports C++14 and most of C++17 
[color=#434f54]/// Signum function: [/color][u][color=#434f54]https://stackoverflow.com/a/4609795[/color][/u]
[color=#5e6d03]template[/color] [color=#434f54]<[/color][color=#5e6d03]typename[/color] T[color=#434f54]>[/color]
[color=#00979c]int[/color] sgn(T val) { [color=#5e6d03]return[/color] (T(0) [color=#434f54]<[/color] val) [color=#434f54]-[/color] (val [color=#434f54]<[/color] T(0)); }
[color=#434f54]/// Bisection method for rootfinding:[/color]
[color=#434f54]/// Passing a functor as an argument to another function[/color]
[color=#5e6d03]template[/color] [color=#434f54]<[/color][color=#00979c]class[/color] Functor[color=#434f54]>[/color]
[color=#00979c]float[/color] findRoot(Functor func, [color=#00979c]float[/color] lowerbound, [color=#00979c]float[/color] upperbound, [color=#00979c]float[/color] tolerance [color=#434f54]=[/color] 1e-3, [color=#00979c]unsigned[/color] [color=#00979c]int[/color] maxIterations [color=#434f54]=[/color] 100) {
[color=#5e6d03]while[/color] (maxIterations [color=#434f54]--[/color][color=#434f54]>[/color] 0) {
[color=#00979c]float[/color] midPoint [color=#434f54]=[/color] (lowerbound [color=#434f54]+[/color] upperbound) [color=#434f54]/[/color] 2;
[color=#5e6d03]if[/color] ((upperbound [color=#434f54]-[/color] lowerbound) [color=#434f54]/[/color] 2 [color=#434f54]<[/color] tolerance [color=#434f54]||[/color] func(midPoint) [color=#434f54]==[/color] 0)
[color=#5e6d03]return[/color] midPoint;
[color=#5e6d03]if[/color] (sgn(func(midPoint)) [color=#434f54]==[/color] sgn(func(lowerbound)))
lowerbound [color=#434f54]=[/color] midPoint;
[color=#5e6d03]else[/color]
upperbound [color=#434f54]=[/color] midPoint;
}
[b][color=#d35400]Serial[/color][/b][color=#434f54].[/color][color=#d35400]println[/color](F([color=#005c5f]"Bisection failed to reach tolerance, increase number of iterations"[/color]));
[color=#5e6d03]return[/color] (lowerbound [color=#434f54]+[/color] upperbound) [color=#434f54]/[/color] 2;
}
[color=#434f54]/// A lambda returning another lambda[/color]
[color=#00979c]auto[/color] makeQuadraticPolynomial [color=#434f54]=[/color] [[color=#000000]]([/color][color=#00979c]float[/color] a0, [color=#00979c]float[/color] a1, [color=#00979c]float[/color] a2) {
[color=#5e6d03]return[/color] [[color=#434f54]=[/color][color=#000000]]([/color][color=#00979c]float[/color] x) {
[color=#5e6d03]return[/color] a2 [color=#434f54]*[/color] x [color=#434f54]*[/color] x [color=#434f54]+[/color] a1 [color=#434f54]*[/color] x [color=#434f54]+[/color] a0;
};
};
[color=#5e6d03]#if[/color] __cplusplus [color=#434f54]>=[/color] 201402L
[color=#434f54]/// In C++14, you can easily return lambdas from normal functions[/color]
[color=#00979c]auto[/color] makeQuadraticPolynomialCpp14([color=#00979c]float[/color] a0, [color=#00979c]float[/color] a1, [color=#00979c]float[/color] a2) {
[color=#5e6d03]return[/color] [[color=#434f54]=[/color][color=#000000]]([/color][color=#00979c]float[/color] x) {
[color=#5e6d03]return[/color] a2 [color=#434f54]*[/color] x [color=#434f54]*[/color] x [color=#434f54]+[/color] a1 [color=#434f54]*[/color] x [color=#434f54]+[/color] a0;
};
}
[color=#5e6d03]#endif[/color]
[color=#00979c]float[/color] goldenQuadratic([color=#00979c]float[/color] x) {
[color=#5e6d03]return[/color] x [color=#434f54]*[/color] x [color=#434f54]-[/color] x [color=#434f54]-[/color] 1;
}
[color=#00979c]void[/color] [color=#5e6d03]setup[/color]() {
[b][color=#d35400]Serial[/color][/b][color=#434f54].[/color][color=#d35400]begin[/color](115200);
[color=#5e6d03]while[/color] ([color=#434f54]![/color][b][color=#d35400]Serial[/color][/b]);
[color=#434f54]// Create a lambda function that evaluates a polynomial[/color]
[color=#00979c]float[/color] a2 [color=#434f54]=[/color] 2, a1 [color=#434f54]=[/color] 0, a0 [color=#434f54]=[/color] [color=#434f54]-[/color]4;
[color=#00979c]auto[/color] polynomial1 [color=#434f54]=[/color] [[color=#434f54]=[/color][color=#000000]]([/color][color=#00979c]float[/color] x) {
[color=#5e6d03]return[/color] a2 [color=#434f54]*[/color] x [color=#434f54]*[/color] x [color=#434f54]+[/color] a1 [color=#434f54]*[/color] x [color=#434f54]+[/color] a0;
};
[color=#00979c]float[/color] root1 [color=#434f54]=[/color] findRoot(polynomial1, 0, 2, 1e-6);
[b][color=#d35400]Serial[/color][/b][color=#434f54].[/color][color=#d35400]print[/color]([color=#005c5f]"The first polynomial has a root in "[/color]);
[b][color=#d35400]Serial[/color][/b][color=#434f54].[/color][color=#d35400]println[/color](root1, 6);
[color=#434f54]// Create a lambda function that saves the coefficients[/color]
[color=#00979c]auto[/color] polynomial2 [color=#434f54]=[/color] makeQuadraticPolynomial([color=#434f54]-[/color]3, 0, 1);
[color=#00979c]float[/color] root2 [color=#434f54]=[/color] findRoot(polynomial2, 0, 2, 1e-6);
[b][color=#d35400]Serial[/color][/b][color=#434f54].[/color][color=#d35400]print[/color]([color=#005c5f]"The second polynomial has a root in "[/color]);
[b][color=#d35400]Serial[/color][/b][color=#434f54].[/color][color=#d35400]println[/color](root2, 6);
[color=#434f54]// You can also use a normal function pointer[/color]
[color=#00979c]float[/color] root3 [color=#434f54]=[/color] findRoot(goldenQuadratic, 1, 2, 1e-6);
[b][color=#d35400]Serial[/color][/b][color=#434f54].[/color][color=#d35400]print[/color]([color=#005c5f]"Φ = "[/color]);
[b][color=#d35400]Serial[/color][/b][color=#434f54].[/color][color=#d35400]println[/color](root3, 6);
}
[color=#00979c]void[/color] [color=#5e6d03]loop[/color]() {}
On platforms that have access to the STL, you can create an std::function
from a lambda. This has some overhead, though, because the actual functor inside of the std::function is type-erased, IIRC.
This is used extensively in the ESP8266 and ESP32 cores for callbacks etc.