[Development] Passing QSize, QPoint, QTime and other small structs by value

Matthew Woehlke mwoehlke.floss at gmail.com
Mon Apr 10 22:27:05 CEST 2017


On 2017-04-07 06:40, Sergio Martins wrote:
> Some time ago I documented the guidelines on passing by value vs
> const-ref:
> https://wiki.qt.io/API_Design_Principles#Passing_by_const-ref_vs._Passing_by_value
> 
> But the reality is that passing small structs by value is not very
> popular and Qt's API mostly passes them by const-ref.
> 
> 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.
> *[1]
> - 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 [2] for extensive research.

Option 2: Teach C++ to choose automatically if such types should be
passed by value or by const-reference.

This has the benefit that it can DTRT even when TRT varies by platform.

(Option 3: Add a template class that implements Option 2? Is this possible?)

-- 
Matthew



More information about the Development mailing list