On Wednesday, 29 July 2015 02:18:33 UTC+3, Alf P. Steinbach wrote:
> On 28-Jul-15 11:38 PM, Paul wrote:
> > are std::unique_ptr or raw pointers ok besides the author's use of shared pointers?
>
> I can't think of any application of a DIY linked list where smart
> pointers would be appropriate for linking up the nodes. At the design
> level it's inappropriate because the linked list is an owner of its
> nodes. At the coding/implementation level it's inappropriate because
> it's more verbose and hence less maintainable code, and also (but
> secondary) just needless memory and possibly execution time overhead.
There may be cases where such design makes best sense and when it
makes sense then it usually simplifies coding as well. I can think of
two:
1) Case when element is most logical owner of next element.
Singly linked list is technically subcase of tree. Sort of one-branched tree.
If trunk interacts with its branch then a reference makes sense. If trunk
owns the branch then smart pointer makes sense as that reference.
2) Case when neither elements nor the list own elements.
Elements are owned by something outside but sometimes form such
temporary lists. Intrusive list may perform better and make better sense
than list of pointers. Non-owning 'std::unique_ptr' may be used there as
link between elements to signal the "something outside" that element has
left such list.
Performance-wise there is very little difference between 'unique_ptr' and
raw ... it is 'shared_ptr' that is fat.