C++11 Lambda Expressions as Callback Functions

c++, c++11, callback, lambda, user-interface

Solution

Does any C++ GUI toolkit out there support definition of callback functions as C++11 lambda expressions?

If they accept function pointers then you can at least use lambdas that don't capture anything. Such lambdas can be automatically converted to function pointers.

What type signature should I use for functions taking lambda expressions as arguments and how does these support implicit conversions?

If you want people to use lambdas or any callable object then you could either have your API accept std::function objects, or use a template:

template<typename Callback>
void do_it(Callback c) {
    c();
}

do_it([&]{ c = a+b; });

A template will allow the lambda to be inlined while std::function require indirection. This may not matter much for GUI callbacks.

Problem

Does any C++ GUI toolkit out there support definition of callback functions as C++11 lambda expressions? I believe this is a unique pro of using C# (compared to C++ at least) for writing GUI-based programs. What type signature should I use for functions taking lambda expressions as arguments and how does these support implicit conversions?

Original source