So as a follow up to my previous message.....

> > > You might actually have a bit of difficulty using a lambda function as a 
> > > callback, because to use some of their more powerful features (like 
> > > taking variable callback data) I *think* the FLTK callback functions 
> > > will have to be modified slightly. However, if I abuse the language for 
> > > a little bit, something like this *should* work (read: code is 
> > > completely untested and will not compile - it's an example and that is 
> > > left as an exercise for the reader!):
> > > 
> > > my_button1("Button!")
> > > my_button2.callback(<>(Fl_Widget* w) -> void {my_button1.label("No soup 
> > > for you");});
> > > 
> > > You probably couldn't use "auto" as the return type here (nor decltype), 
> > > as I think FLTK explicitly expects void, but you can theoretically do 
> > > anything you want if you twist the function in the right way. Note that 
> > > you can use it to call my_button1.label because of the magic of lambdas 
> > > (they're translated into objects to make this possible)
> 
>       Cool.
> > 
> > I see, so although the above is untested, I take it the implication
> > is variables (and perhaps methods) within the scope of the
> > lambda's declaration will automatically be available whenever
> > the lambda is invoked?
> > 
> > So perhaps, if this code were all in a class, do you think the
> > lambda code would have access to the class instance's variables/methods
> > auto-magically? eg:
>  > 
> > class MyClass : public Fl_Window {
> >     Fl_Button *my_button1;
> >     Fl_Button *my_button2;
> >     ..
> >     MyClass() {
> >         ..
> >         my_button2->callback( <>(Fl_Widget*w) -> void 
> > {my_button1->label("xxx");} ); // <-- 'my_button1' visible?
> >         ..
> >     }
> > };
> > 
> >     If something like that works, I imagine the language handles
> >     preserving 'this' so that it's set invisibly whenever the
> >     lambda is called, which syntactically would be very neat-o.
> > Alright, it looks like this stupid phone is going to force me to top-post, 
> > so apologies in advance.
> > 
> > 
> Yes, I was simply trying to get the 
implication across that you can pass variables through the scope of the 
lambda - but now I think of it, it *may* also require the use of 
extern(my_button1) inside my example lambda, to force it to be used as a
 reference.
> In answer to your second question, the "this" 
pointer is preserved across the lambda (AFAIK). Lambda functions really 
only serve as syntactic sugar....
>

The "this" pointer is preserved across the lambda expression, however it should 
really be explicitly "captured".
As an aside, my early-morning haste meant I somewhat screwed up - the lambda 
isn't declared through <>, it's declared through []. <> was an earlier working 
draft. Apologies for this.

In any case, to explicitly keep the "this" pointer, you simply pass the word 
"this" into the brackets - this is similar of any variable you wish to make a 
copy of, though "this" isn't copied.

my_button2->callback( [this] () { this->my_button1->label("xxx");} ); // I 
haven't specified the return type here; it will default to void. You could just 
as easily use "[this] () -> void {...}"

It should still work without it, but I can't remember whether or not it's a 
pointer to a *copy* of the original object or not.

You can force references through an ampersand in the brackets, and there are a 
whole range of options you can use.
See http://www.cprogramming.com/c++11/c++11-lambda-closures.html for a more 
complete list and a few decent examples, if not somewhat contrived. 

Regards,
Ben


                                          
_______________________________________________
fltk mailing list
[email protected]
http://lists.easysw.com/mailman/listinfo/fltk

Reply via email to