Title
Inconsistency between unique_ptr and shared_ptr
Status
c++17
Section
[util.smartptr.shared.const]
Submitter
Jonathan Wakely

Created on 2014-07-03.00:00:00 last changed 90 months ago

Messages

Date: 2015-01-19.20:08:19

Proposed resolution:

This wording is relative to N4296.

  1. Change [util.smartptr.shared.const] p29 as indicated:

    template <class Y, class D> shared_ptr(unique_ptr<Y, D>&& r);
    

    […]

    -29- Effects: If r.get() == nullptr, equivalent to shared_ptr(). Otherwise, if D is not a reference type, equivalent to shared_ptr(r.release(), r.get_deleter()). Otherwise, equivalent to shared_ptr(r.release(), ref(r.get_deleter()))Equivalent to shared_ptr(r.release(), r.get_deleter()) when D is not a reference type, otherwise shared_ptr(r.release(), ref(r.get_deleter())).

Date: 2015-01-18.00:00:00

[ 2015-01-18 Library reflector vote ]

The issue has been identified as Tentatively Ready based on eight votes in favour.

Date: 2014-07-03.00:00:00

unique_ptr guarantees that it will not invoke its deleter if it stores a null pointer, which is useful for deleters that must not be called with a null pointer e.g.

unique_ptr<FILE, int(*)(FILE*)> fptr(file, &::fclose);

However, shared_ptr does invoke the deleter if it owns a null pointer, which is a silent change in behaviour when transferring ownership from unique_ptr to shared_ptr. That means the following leads to undefined behaviour:

std:shared_ptr<FILE> fp = std::move(fptr);

Peter Dimov's suggested fix is to construct an empty shared_ptr from a unique_ptr that contains a null pointer.

History
Date User Action Args
2017-07-30 20:15:43adminsetstatus: wp -> c++17
2015-05-22 18:31:21adminsetstatus: ready -> wp
2015-01-18 18:40:08adminsetmessages: + msg7216
2015-01-18 18:40:08adminsetstatus: new -> ready
2014-07-03 22:51:54adminsetmessages: + msg7092
2014-07-03 00:00:00admincreate