Skip to main content
std::function is supported on ARM
Source Link
Edgar Bonet
  • 45.2k
  • 4
  • 42
  • 81

std::function is not supportednot supported by the Arduino environment. (Edit: as mentioned by the Arduino environmentKIIV in a comment, it's supported on ARM targets, but not on AVR.) You can, however however, pass a non-capturing lambda to to a function that expects a plain function 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...

std::function is not supported by the Arduino environment. 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...

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...

Source Link
Edgar Bonet
  • 45.2k
  • 4
  • 42
  • 81

std::function is not supported by the Arduino environment. 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...