Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++11 Lambda Expressions as Callback Functions

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?

like image 438
Nordlöw Avatar asked Apr 05 '12 17:04

Nordlöw


People also ask

Is a lambda function a callback function?

The callback function takes two arguments: an Error and a response. When you call it, Lambda waits for the event loop to be empty and then returns the response or error to the invoker. The response object must be compatible with JSON. stringify .

Can a lambda closure be used to create a C++11 thread?

Can you create a C++11 thread with a lambda closure that takes a bunch of arguments? Yes – just like the previous case, you can pass the arguments needed by the lambda closure to the thread constructor.

What is lambda expression in C++11?

C++11 introduces lambdas allow you to write an inline, anonymous functor to replace the struct f . For small simple examples this can be cleaner to read (it keeps everything in one place) and potentially simpler to maintain, for example in the simplest form: void func3(std::vector<int>& v) { std::for_each(v.

Can lambdas be templated?

Lambda-expressions are not allowed in unevaluated expressions, template arguments, alias declarations, typedef declarations, and anywhere in a function (or function template) declaration except the function body and the function's default arguments.


3 Answers

The answer to second part of the question: You could use std::function<Signature> where Signature = e.g. void (int) or - if the lambdas don't take closures - the good old void (Foo*)(int) method, since a lambda without a closure must be convertible to proper function type. So, for example a call to a method with signature:

void AddHandler(std::function<void (int)> const &);

could look like this:

myObject.AddHandler([&](int _) {
    // do something and access captured variables by reference
});
like image 198
Paul Michalik Avatar answered Oct 21 '22 08:10

Paul Michalik


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.

like image 34
bames53 Avatar answered Oct 21 '22 09:10

bames53


There is a new GUI toolkit that meets your requirement. It is trying to provide many features for improving development efficiency.

An introduction to the library. http://nanaproject.wordpress.com/2011/12/16/preliminary-study-of-nana-c-library/

An article to explain how to reduce complexity by using the library and std::bind together. http://nanaproject.wordpress.com/2012/01/31/idioms-and-insights-for-a-good-design/

Have fun!

like image 41
Jinhao Avatar answered Oct 21 '22 09:10

Jinhao