Can I use anonimous (lambda) functins in MBED?

I’m trying to assign a function to a variable without the function declaration.

So this code is OK (compiling):

void foo(void) {
         a++;
}

typedef void (*tCallBack)();
tCallBack cb;
cb = &foo;

However this code has extra lines and not very readable as the action declared in the separate place (outside of main). So I’d like to do something like this:

typedef void (*tCallBack)();
tCallBack cb;
cb = [](){ a++; };

But the compiller throws an error: Error: Expected an expression in “main.cpp”, Line: 254, Col: 25

The syntax {} is taken from wikipedia and should work for C++ 11 and above. As I know - MBED uses C++ 11 so it should work.

Any thoughts?

Hi Roman,
We see that you cross-posted this to the Questions area and got a good response from Zoltan. For the merit of these reading this post, the Online Compiler was recently bumped up to use Arm Compiler 6 but it doesn’t support C++ 11. It’s still on C++98. If you take your project to the Mbed CLI, we’ve upgrade the IAR support to v8.32 and that has moved to C++14.
Regards,
Ralph, Team Mbed

1 Like

Hello, Ralph!
Do you have a plan to upgrade online compiller to C++ 11?