Home » Community » U++ community news and announcements » 2023.2
Re: 2023.2 [message #60311 is a reply to message #60305] |
Thu, 16 November 2023 13:03   |
 |
koldo
Messages: 3437 Registered: August 2008
|
Senior Veteran |
|
|
mirek wrote on Wed, 15 November 2023 10:41koldo wrote on Wed, 15 November 2023 08:38Hi Mirek
These sentences are very interesting.
Quote:Also, make sure that all other timer (and GUI in general) lambdas are with [=]....
You should only use [&] if you are sure that lambda is invoked immediately (e.g. with CoDo).
I almost understand why you say that but, for didactic reasons, could you explain why to include capture by value rather than by reference.
struct MyApp ... {
Button btn;
String a;
...
};
void MyApp::MyApp()
{
String b;
btn << [&] { a = b; };
}
This creates a dangling reference to b. In fact, if the lambda execution is not 'immediate' (before next statement basically), in most of GUI cases it makes only sense to work with member variables (e.g. 'a' in this case). But then, [=] in fact captures 'this' and that is likely MORE efficient than capturing all member variables individually.
Note that capturing this by [=] was deemed confusing by C++ comittee and they tried to improve on it with [=, this], unfortunately currently it is hard to make code compatible both ways.
Mirek
Thank you Mirek for answering this off-topic question.
Additional information about this can be found here.
I didn't know this:Quote:[=] in fact captures 'this'
In summary, especially in classes, when we sometimes put a [&], we should really have to put [this], and in some cases a [=].
Best regards
IƱaki
|
|
|
 |
|
2023.2
By: mirek on Tue, 24 October 2023 11:17
|
 |
|
Re: 2023.2
By: Tom1 on Tue, 24 October 2023 14:27
|
 |
 |
Re: 2023.2
|
 |
|
Re: 2023.2
By: zsolt on Tue, 24 October 2023 23:19
|
 |
|
Re: 2023.2
By: Klugier on Wed, 25 October 2023 00:00
|
 |
|
Re: 2023.2
|
 |
|
Re: 2023.2
By: mirek on Wed, 25 October 2023 09:47
|
 |
|
Re: 2023.2
By: Tom1 on Tue, 14 November 2023 15:30
|
 |
|
Re: 2023.2
By: mirek on Tue, 14 November 2023 15:48
|
 |
|
Re: 2023.2
By: Tom1 on Tue, 14 November 2023 16:00
|
 |
|
Re: 2023.2
By: Tom1 on Tue, 14 November 2023 16:07
|
 |
|
Re: 2023.2
By: mirek on Tue, 14 November 2023 17:17
|
 |
|
Re: 2023.2
By: Tom1 on Tue, 14 November 2023 17:49
|
 |
|
Re: 2023.2
By: mirek on Tue, 14 November 2023 23:48
|
 |
|
Re: 2023.2
By: koldo on Wed, 15 November 2023 08:38
|
 |
|
Re: 2023.2
By: mirek on Wed, 15 November 2023 10:41
|
 |
|
Re: 2023.2
By: koldo on Thu, 16 November 2023 13:03
|
 |
|
Re: 2023.2
By: Tom1 on Wed, 15 November 2023 11:14
|
 |
|
Re: 2023.2
By: mirek on Wed, 15 November 2023 12:17
|
 |
|
Re: 2023.2
By: Tom1 on Wed, 15 November 2023 13:07
|
Goto Forum:
Current Time: Sat Jul 05 23:33:45 CEST 2025
Total time taken to generate the page: 0.03452 seconds
|