[Development] Passing QSize, QPoint, QTime and other small structs by value
sergio.martins at kdab.com
Fri Apr 7 12:40:38 CEST 2017
Some time ago I documented the guidelines on passing by value vs
This was discussed in #qt-labs at the time and informally +2'd there.
But the reality is that passing small structs by value is not very
popular and Qt's API mostly passes them by const-ref.
I think this is a good time to have a more formal conversation,
bikeshed+popcorn, then write a QUIP and hopefully change the API for Qt
The guideline is very simple:
- If your type has a non-trivial copy-CTOR or non-trivial DTOR then pass
by const-ref, no matter how small it is, to avoid calling those methods.
- If your type has trivial CTOR and trivial DTOR, pass by value if it's
small, otherwise const-ref. Small meaning <= 16 bytes.
The idea is that by value allows the struct's members to be passed in
CPU registers, see  for extensive research.
The good news is, even if you don't care about raw performance, passing
by value is more convenient, as you type less code, so IMO, more fun to
 - Shared pointers should go by value though, I'll try to prove it in
Sérgio Martins | sergio.martins at kdab.com | Senior Software Engineer
Klarälvdalens Datakonsult AB, a KDAB Group company
Tel: Sweden (HQ) +46-563-540090, USA +1-866-777-KDAB(5322)
KDAB - The Qt, C++ and OpenGL Experts
More information about the Development