Signal-slot and rvalue references
-
Hi!
I would like to ask how exactly the signal-slot mechanism is implemented under the hood, and whether it makes use of rvalue references if I toss one at it. I only ask because I am developing an application that will pass potentially large objects around, or simply ones that take time to initialize. I do know however that these objects will be destroyed once the emit signal has been issued, and I would like to make use of move ctors, either those that I write, or those that are predefined by Qt (if there are any). Custom made ctors would include GPU mem object wrapper classes, and I would expect objects lie QByteStream and alike to have their own.
Are these feature supported, or is passing std::shared_ptr (or QSharedPointer) the best way to pass on large objects like this?
-
You can pass all the types of variables in a signal, just as if it were directly calling the function. One important thing to note while using signals across threads in asynchronous mode is that the variables better be passes by value, or else you will need to use an eplicit mechanism to make the passed data valid until all the slots are executed.
-
And how does multiple slots connected to a signal work? If I understand it correctly, hooking up multiple slots to a signal is valid even when using RValue refs, it will only result in erronous behavior, since after the first invocation, the object has already had it's contents moved out.
Passing references I guess is ok, so long as the object passed will not be destroyed (eg. it is a member of the wrapping object) until the actual call is finished.
-
bq. Passing references I guess is ok, so long as the object passed will not be destroyed (eg. it is a member of the wrapping object) until the actual call is finished.
Correct