Does mbed has Lambda expression?

Does mbed has Lambda expression?


Yes, you can use lambda expressions in your mbed code.
For example:

#include "mbed.h"

class MyInterruptIn : public InterruptIn
    PinName _pin;
    MyInterruptIn(PinName pin) : InterruptIn(pin), _pin(pin) {}
    PinName pin() { return _pin; }

MyInterruptIn* myPins[] = {
    new MyInterruptIn(p14),
    new MyInterruptIn(p15),
    new MyInterruptIn(p16)

DigitalOut      led(LED1);
Thread          thread;
EventQueue      eventQueue;

int main() {
    thread.start(callback(&eventQueue, &EventQueue::dispatch_forever));

    for (int i = 0; i < (sizeof(myPins) / sizeof(*myPins)); i++) {
        // lambda expression:
        myPins[i]->rise([i]() {, "Pin %d triggered!\r\n", myPins[i]->pin()); });

    while(1) {
        led = !led;

to build on @hudakz answer, you can pass lambda directly as rvalues just like he did, or you can declare them before using them, like:

for (int i = 0; i < (sizeof(myPins) / sizeof(*myPins)); i++) {

    auto print_triggered_pin = [&i] {, "Pin %d triggered!\r\n", myPins[i]->pin());



note that the () can be omitted if not used.

This is really useful is you want to give meaningful names to difficult part of the code, such as low level bits juggling etc.