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?
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 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.
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.
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.
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
});
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.
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With