[Development] Copy initialization vs. direct initialization?

Giuseppe D'Angelo giuseppe.dangelo at kdab.com
Thu Mar 26 08:50:47 CET 2026


Hi,

Il 26/03/26 07:13, Sze Howe Koh ha scritto:
> Does the Qt Project have a preference between the following?
> 
>      QVector2D myVector = QVector2D(x, y);
> 
> vs.
> 
>      QVector2D myVector(x, y);
> 
> Context: https://codereview.qt-project.org/c/qt/qtdeclarative/+/642149
> used the former;
> https://codereview.qt-project.org/c/qt/qtdeclarative/+/724447 copies
> the older patch's logic while trying the latter syntax. I plan to make
> them consistent with each other, but I don't know which way is
> preferred.
> 
> https://wiki.qt.io/Qt_Coding_Style and
> https://wiki.qt.io/Coding_Conventions don't discuss this.

No, it's up to the author's preferences, hopefully by mimicking the 
surrounding style. There's a minor preference towards using copy 
initialization in general and/or braced initialization (in order to 
avoid the most vexing parse), but again it's up to you. To be honest I 
don't think anyone would write `QVector2D myvector = {x, y}` (I 
certainly wouldn't).

Do note that the two forms you posted above are absolutely equivalent, 
and in neither case there's copy initialization.

[insert meme on how many different syntaxes C++ has for initialization 
and how each one is subtly broken]


Thank you,
-- 
Giuseppe D'Angelo | giuseppe.dangelo at kdab.com | Senior Software Engineer
KDAB (France) S.A.S., a KDAB Group company
Tel. France +33 (0)4 90 84 08 53, http://www.kdab.com
KDAB - Trusted Software Excellence
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 4850 bytes
Desc: Firma crittografica S/MIME
URL: <http://lists.qt-project.org/pipermail/development/attachments/20260326/0f65ec66/attachment.bin>


More information about the Development mailing list