user45493: I want to pass a lambda function as an argument to a method. Example:
T reduce(const T initial, const std::function<T(T, T)> acc) {
    T value = initial;
    for (size_t i = 0; i < S; ++i) {
        value = acc(value, this->values[i]);
    }

    return value;
}

...

int sum = this->reduce(42, [](int acc, int value) { return acc + value; });

If I try to compile this code for Arduino, I get an obvious error:

error: 'function' in namespace 'std' does not name a template type

How can I use a lambda function in Arduino?

user7508: std::function is not supported by the Arduino environment.
(Edit: as mentioned by KIIV in a comment, it's supported on ARM
targets, but not on AVR.) You can, however, pass a non-capturing lambda
to a function that expects a plain function pointer. For example, given
T reduce(const T initial, T (*acc)(T, T)) {
    T value = initial;
    for (size_t i = 0; i < S; ++i) {
        value = acc(value, this->values[i]);
    }
    return value;
}


You can call it as
int sum = test.reduce(42, [](int acc, int value){ return acc + value; });

If you want your lambda to capture something, it would seem you are out
of luck...

45493: <p>I want to pass a lambda function as an argument to a method. Example:</p>
<pre class="lang-cpp prettyprint-override"><code>T reduce(const T initial, const std::function&lt;T(T, T)&gt; acc) {
    T value = initial;
    for (size_t i = 0; i &lt; S; ++i) {
        value = acc(value, this-&gt;values[i]);
    }

    return value;
}

...

int sum = this-&gt;reduce(42, [](int acc, int value) { return acc + value; });
</code></pre>
<p>If I try to compile this code for Arduino, I get an obvious error:</p>
<blockquote>
<p>error: 'function' in namespace 'std' does not name a template type</p>
</blockquote>
<p>How can I use a lambda function in Arduino?</p>
 7508: <p><code>std::function</code> is <s>not supported by the Arduino environment</s>.
(<strong>Edit</strong>: as mentioned by KIIV in a comment, it's supported on ARM
targets, but not on AVR.) You can, however, pass a non-capturing lambda
to a function that expects a plain function pointer. For example, given</p>
<pre class="lang-cpp prettyprint-override"><code>T reduce(const T initial, T (*acc)(T, T)) {
    T value = initial;
    for (size_t i = 0; i &lt; S; ++i) {
        value = acc(value, this-&gt;values[i]);
    }
    return value;
}

</code></pre>
<p>You can call it as</p>
<pre class="lang-cpp prettyprint-override"><code>int sum = test.reduce(42, [](int acc, int value){ return acc + value; });
</code></pre>
<p>If you want your lambda to capture something, it would seem you are out
of luck...</p>
