PDA

View Full Version : Lambda callback functions



wayfaerer
12th March 2012, 04:17
In this article:

http://byuu.org/articles/qt

the author (who doesn't much like Qt) seems to claim that signals and slots could be replaced by lambdas, e.g.,


saveButton.onClick = []() { file.saveToDisk(); };

I don't understand much about lambdas or C++11, but I am curious whether or not this is true, as the code presented in the article does look nice.

FWIW I like signals and slots, and plan to continue using them... I'm just curious about this.

ChrisW67
12th March 2012, 05:36
He or she is entitled to their opinion. Seriously though, that's quite a rant. A cursory glance screams at me that the author was trying to make Qt do things the way he/she had always done them. This is a recipe for annoyance in any language. There's quite a collection of half truths and misunderstandings to boot.

Sure, you can possibly do call backs using C++11 lambdas (or Standard library function wrappers, or Boost equivalents), but you certainly couldn't do that in 2005, or on every compiler, or on every platform. You can't do it cross-thread with queuing and argument marshalling where required.

I think you would do yourself a favour by forming your own opinions.

wayfaerer
12th March 2012, 06:10
Yeah, I know it was rantish, and I didn't buy into it... I really like Qt. It was just that one line that caught my eye. I don't know if the lambda callbacks would actually be that easy or not.

wysota
12th March 2012, 11:33
It's likely Qt5 will support lambdas as slots when used with a C++11 compliant compiler.

About the "article" itself, I always laugh at discussions like that where people assume everyone does things the way they do :) At times like this I'm willing to put such person in front of MSVC6 asking them to emulate what Qt's container classes do with MSVC6's implementation of STL and then to repeat that with a compiler that doesn't have an STL implementation at all.