From kde-core-devel Mon Apr 29 23:01:36 2013 From: Milian Wolff Date: Mon, 29 Apr 2013 23:01:36 +0000 To: kde-core-devel Subject: Re: Initialization of QSharedDataPointer's for empty objects (i.e. Foo::Foo(void)) Message-Id: <15541471.LLbQ4kSKbW () minime> X-MARC-Message: https://marc.info/?l=kde-core-devel&m=136727652121073 MIME-Version: 1 Content-Type: multipart/mixed; boundary="--nextPart1929980.9m2YljqxF4" --nextPart1929980.9m2YljqxF4 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" On Monday 29 April 2013 15:16:29 Thiago Macieira wrote: > On segunda-feira, 29 de abril de 2013 21.52.37, Milian Wolff wrote: > > I.e.: In the ctors that construct an "empty" object do not call "ne= w > > Private" but instead (re-)use a static shared empty object created = on the > > stack - see shared_empty and shared_null in qstring.{cpp,h}. >=20 > You probably did not mean what you said: you do not want something al= located > on the stack. You want a global. Like QString :-) Yes of course - thanks for the correction. > To be *really* like QString, you want your private to be POD. That me= ans you > cannot derive from QSharedData, you cannot use QAtomicInt, QString or= > QByteArray. That may be taking it a bit too far. >=20 > If your private class is not POD, you should use a K/Q_GLOBAL_STATIC = instead > to hold your private. No, I don't think making it a POD is required. {K,Q}_GLOBAL_STATIC with= =20 Q_MOVABLE_TYPE should be sufficient in most cases I think. > Examples: Interestingly I can't find a case cases don't seem to be using=20 QSharedDataPointer, or am I missing something? And is the following not OK due to random static initialization order i= n=20 C++03? http://paste.kde.org/734738/ But in C++11 it should be fine, no? The alternative using {K,Q}_GLOBAL_= STATIC=20 is imo much uglier, see: http://paste.kde.org/734750/ I could esp. not make it work with a private Private class as used in t= he=20 previous code snippet. I.e. this: http://paste.kde.org/734756/ triggers= the=20 following compile error: /home/milian/projects/foo/src/main.cpp: In function=20 =E2=80=98QSharedDataPointer* s_sharedEmpty()=E2=80=99: /home/milian/projects/foo/src/main.cpp:21:56: error:=20 =E2=80=98QSharedDataPointer* s_sharedEmpty()=E2=80=99 was= declared =E2=80=98extern=E2=80=99 and=20 later =E2=80=98static=E2=80=99 [-fpermissive] Q_GLOBAL_STATIC(QSharedDataPointer, s_sharedEmpty) ^ /home/milian/projects/foo/src/main.cpp:12:46: error: previous declarati= on of=20 =E2=80=98QSharedDataPointer* s_sharedEmpty()=E2=80=99 [-f= permissive] friend QSharedDataPointer* ::s_sharedEmpty(); Thanks for the useful input Thiago, much appreciated! --=20 Milian Wolff mail@milianw.de http://milianw.de --nextPart1929980.9m2YljqxF4 Content-Type: application/pgp-signature; name="signature.asc" Content-Description: This is a digitally signed message part. Content-Transfer-Encoding: 7Bit -----BEGIN PGP SIGNATURE----- Version: GnuPG v2.0.19 (GNU/Linux) iEYEABECAAYFAlF++9AACgkQDA6yEs0dE5M+BQCfYlpip0xvYCb0UNuYdiwoLAVL 1GMAoJPDuCv/n4fi9NHQnxzbEEp0BLkz =7v6L -----END PGP SIGNATURE----- --nextPart1929980.9m2YljqxF4--