Smart D-Ptr in KCoreAddons

Mark markg85 at gmail.com
Thu Aug 22 23:54:58 UTC 2013


On Thu, Aug 22, 2013 at 6:49 PM, Ivan Čukić <ivan.cukic at kde.org> wrote:
> Hi all,
>
> We had some discussions on plasma-devel regarding the use of a smart non-
> inherited D-Pointer[1] and Kevin proposed moving the pointer into KCoreAddons.
> The only listed downside of including it into plasma is that it is too low
> level and generic to belong to that framework (Sebas).
>
> Cons:
> C1 - they use variadic templates for P4 (noted by Kevin), but that is not
> strictly necessary and the always-catching-up compilers will be covered.
>
> Pros:
> P1 - safety: no access to the raw pointer
> P2 - safety: no accidental initialization errors or anything similar
> P3 - safety: no possible leaks
> P4 - convenience: forwarded constructor arguments (': d(1,2)' instead of ':
>    d(new Private(1,2))')
> P5 - convenience: default constructor works for no-arg Private constructor
>    (nothing instead of 'd(new Private())')
> P6 - convenience: no delete d;
> P7 - they are spiffy (Aaron) :)
>
> The class is header-only, that is, two headers only. It doesn't change binary
> interface of the user class - it is equivalent to a raw pointer; nor the
> library it should be in.
>
> Cheerio,
> Ivan
>
> [1] http://ivan.fomentgroup.org/blog/2013/06/22/d-ptr-the-modern-way/

Hi Ivan,

I have to say, the results of your d_ptr are really neat. So much more
clean to use. Good job there :)

Having said that, i first want to note that you're far more advanced
in C++ then me. That makes it a bit odd for me to post the critic
below since i can absolutely not read the code from [2] or understand
it. Yet i think it's important to know the below before proceeding
pushing the usage of d_ptr in kde.

C++11 made a big mistake. They introduced make_shared, but neglect to
introduce a make_unique version. The latter would do exactly as you
describe only in a C++14 standard manner. Luckily the C++14
make_unique is already made in templates that are full C++11
compatible.

The paper that describes make_unique - and contains an implementation
- can be found here [1]. For reference, the full code to have
make_unique ready to paste in any file is here [2].

Now that i've said the above i'd like to come back on your
implementation. I would like to suggest you to reconsider using it or
pushing your d_ptr for KDE usage. Rather you could use the code from
[2] and put it behind some ifdef magic. If there is no C++14, use that
code otherwise use what C++14 offers. I think this is the better
course to take. Using your approach will cause confusion in a few
years time when we actually have make_unique in C++14, but still find
some "custom created" make_unique version called d_ptr. People will
undoubtedly want to replace it with the proper C++14 call. Please lets
just try to prevent that situation.

I hope i didn't offend you.

Cheers,
Mark

[1] http://isocpp.org/files/papers/N3656.txt
[2] http://paste.kde.org/p6a116ed8/


More information about the Kde-frameworks-devel mailing list