Initialization of QSharedDataPointer's for empty objects (i.e. Foo::Foo(void))

Thiago Macieira thiago at kde.org
Tue Apr 30 00:41:47 BST 2013


On terça-feira, 30 de abril de 2013 01.01.36, Milian Wolff wrote:
> Interestingly I can't find a case  cases don't seem to be using
> QSharedDataPointer, or am I missing something?

Right. None of them use QSharedDataPointer.

You can find uses of that in these changes I uploaded during the weekend:

https://codereview.qt-project.org/54942
https://codereview.qt-project.org/54943

In particular, pay attention to patch 1 in those and in 54941, before 
QLocalePrivate became POD. That's probably what you're looking for.

> And is the following not OK due to random static initialization order in
> C++03? http://paste.kde.org/734738/

It's *not* ok for Qt. I don't remember what KDE rules are now on dynamic 
initialisation. Qt requires all static variables to be POD, or else you need 
to use Q_GLOBAL_STATIC. So the Foo::Private::s_sharedEmpty type is not 
permitted in Qt.

Otherwise, your code is fine.

> But in C++11 it should be fine, no?

No, it doesn't make any difference.

> The alternative using
> {K,Q}_GLOBAL_STATIC is imo much uglier, see: http://paste.kde.org/734750/

Beauty is in the eye of the beholder. I find that prettier.

> 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();

Remove the friendship, move the Private class out of the private: section.

To befriend a static function, the function needs to be forward-declared first. 
But you can't forward-declare the function if it returns a nested structure.

-- 
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
   Software Architect - Intel Open Source Technology Center
      PGP/GPG: 0x6EF45358; fingerprint:
      E067 918B B660 DBD1 105C  966C 33F5 F005 6EF4 5358
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 190 bytes
Desc: This is a digitally signed message part.
URL: <http://mail.kde.org/pipermail/kde-core-devel/attachments/20130429/018d6c15/attachment.sig>


More information about the kde-core-devel mailing list