Initialization of QSharedDataPointer's for empty objects (i.e. Foo::Foo(void))
Milian Wolff
mail at milianw.de
Tue Apr 30 00:01:36 BST 2013
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 "new
> > Private" but instead (re-)use a static shared empty object created on the
> > stack - see shared_empty and shared_null in qstring.{cpp,h}.
>
> You probably did not mean what you said: you do not want something allocated
> 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 means you
> cannot derive from QSharedData, you cannot use QAtomicInt, QString or
> QByteArray. That may be taking it a bit too far.
>
> 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
Q_MOVABLE_TYPE should be sufficient in most cases I think.
> Examples:
<snip>
Interestingly I can't find a case cases don't seem to be using
QSharedDataPointer, or am I missing something?
And is the following not OK due to random static initialization order in
C++03? http://paste.kde.org/734738/
But in C++11 it should be fine, no? The alternative using {K,Q}_GLOBAL_STATIC
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 the
previous code snippet. I.e. this: http://paste.kde.org/734756/ triggers the
following compile error:
/home/milian/projects/foo/src/main.cpp: In function
‘QSharedDataPointer<Foo::Private>* s_sharedEmpty()’:
/home/milian/projects/foo/src/main.cpp:21:56: error:
‘QSharedDataPointer<Foo::Private>* s_sharedEmpty()’ was declared ‘extern’ and
later ‘static’ [-fpermissive]
Q_GLOBAL_STATIC(QSharedDataPointer<Foo::Private>, s_sharedEmpty)
^
/home/milian/projects/foo/src/main.cpp:12:46: error: previous declaration of
‘QSharedDataPointer<Foo::Private>* s_sharedEmpty()’ [-fpermissive]
friend QSharedDataPointer<Foo::Private>* ::s_sharedEmpty();
Thanks for the useful input Thiago, much appreciated!
--
Milian Wolff
mail at milianw.de
http://milianw.de
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 198 bytes
Desc: This is a digitally signed message part.
URL: <http://mail.kde.org/pipermail/kde-core-devel/attachments/20130430/35203dca/attachment.sig>
More information about the kde-core-devel
mailing list