Smart D-Ptr in KCoreAddons

Ivan Čukić ivan.cukic at kde.org
Fri Aug 23 06:49:36 UTC 2013


> 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].

Make unique is only a method to create a std::unique_ptr, and it doesn't 
provide (or, rather, inhibit) the features d_ptr does. Namely, d_ptr uses 
unique_ptr for most of its logic, but makes the interface to the class slimmed 
down and safer - tailored exactly for this case.

It removes:
 - modifiers
 - get() and friends
 - operator bool
 - operator*
 - comparison operators
 - possibility to be null

It mostly makes the interface and behaviour look as if it was no pointer at 
all, apart from accessing members with d->.

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

std::unique_ptr with make_unique does not provide P1, P2, P5 and P4.
For P4, you would need to write d(make_unique<Private>(...)) in order for it 
to work instead of d(...), which is not much cleaner than d(new Private(...)).


Cheerio,
Ivan


-- 
While you were hanging yourself on someone else's words
Dying to believe in what you heard
I was staring straight into the shining sun



More information about the Kde-frameworks-devel mailing list