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

Alexander Nassian nassian at bitshift-dynamics.com
Fri Apr 7 13:39:22 CEST 2017


I don't care, I always pass const-ref ;)

Beste Grüße / Best regards,
Alexander Nassian

> Am 07.04.2017 um 13:30 schrieb Konstantin Tokarev <annulen at yandex.ru>:
> 
> 
> 
> 07.04.2017, 13:41, "Sergio Martins" <sergio.martins at kdab.com>:
>> Hi,
>> 
>> 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
>> 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
>> 6.
>> 
>> 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.
> 
> I think such rule should be enforced by tooling, e.g. something like clazy.
> 
> Btw, do you consider case when properties of type in question are changed
> in future Qt version, e.g. new fields or ctors are added (in case of non-public
> class/struct)?
> 
>> 
>> The idea is that by value allows the struct's members to be passed in
>> CPU registers, see [2] 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
>> use.
>> 
>> [1] - Shared pointers should go by value though, I'll try to prove it in
>> the QUIP.
>> [2] -
>> http://www.macieira.org/blog/2012/02/the-value-of-passing-by-value/
>> 
>> Cheers,
>> --
>> 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
>> _______________________________________________
>> Development mailing list
>> Development at qt-project.org
>> http://lists.qt-project.org/mailman/listinfo/development
> 
> -- 
> Regards,
> Konstantin
> _______________________________________________
> Development mailing list
> Development at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/development

-- 
 


—

bitshift dynamics GmbH
Neudorfer Str. 1, 79541 Lörrach
Registergericht: Amtsgericht Freiburg i. Breisgau, HRB 713747
Geschäftsführer: Alexander Nassian, Markus Pfaffinger

http://www.bitshift-dynamics.de

Zentrale: +49 762158673 - 0
Fax: +49 7621 58673 - 90

Allgemeine Anfragen: info at bitshift-dynamics.com
Technischer Support: support at bitshift-dynamics.com
Buchhaltung: invoice at bitshift-dynamics.com



More information about the Development mailing list