Title
What is the stored pointer value of an empty weak_ptr?
Status
new
Section
[util.smartptr.weak.const]
Submitter
Casey Carter

Created on 2019-03-15.00:00:00, last changed 2019-06-10.05:16:56.

Messages

Date: 2019-06-10.05:16:56

Proposed resolution:

This wording is relative to N4810.

  1. Modify [util.smartptr.weak.const] as indicated (note the drive-by edit to cleanup the occurrences of "constructs an object of class foo"):

    constexpr weak_ptr() noexcept;
    

    -1- Effects: Constructs an empty weak_ptr object that stores a null pointer value.

    -2- Ensures: use_count() == 0.

    weak_ptr(const weak_ptr& r) noexcept;
    template<class Y> weak_ptr(const weak_ptr<Y>& r) noexcept;
    template<class Y> weak_ptr(const shared_ptr<Y>& r) noexcept;
    

    -3- Remarks: The second and third constructors shall not participate in overload resolution unless Y* is compatible with T*.

    -4- Effects: If r is empty, constructs an empty weak_ptr object that stores a null pointer value; otherwise, constructs a weak_ptr object that shares ownership with r and stores a copy of the pointer stored in r.

    -5- Ensures: use_count() == r.use_count().

Date: 2019-06-09.00:00:00

[ 2019-06-09 Priority set to 2 after reflector discussion ]

Date: 2019-03-15.00:00:00

[util.smartptr.weak.const] specifies weak_ptr's default constructor:

constexpr weak_ptr() noexcept;

1 Effects: Constructs an empty weak_ptr object.

2 Ensures: use_count() == 0.

and shared_ptr converting constructor template:

weak_ptr(const weak_ptr& r) noexcept;
template<class Y> weak_ptr(const weak_ptr<Y>& r) noexcept;
template<class Y> weak_ptr(const shared_ptr<Y>& r) noexcept;

3 Remarks: The second and third constructors shall not participate in overload resolution unless Y* is compatible with T*.

4 Effects: If r is empty, constructs an empty weak_ptr object; otherwise, constructs a weak_ptr object that shares ownership with r and stores a copy of the pointer stored in r.

5 Ensures: use_count() == r.use_count().

Note that neither specifies the value of the stored pointer when the resulting weak_ptr is empty. This didn't matter — the stored pointer value was unobservable for an empty weak_ptr — until we added atomic<weak_ptr>. [util.smartptr.atomic.weak]/15 says:

Remarks: Two weak_ptr objects are equivalent if they store the same pointer value and either share ownership, or both are empty. The weak form may fail spuriously. See [atomics.types.operations].

Two empty weak_ptr objects that store different pointer values are not equivalent. We could correct this by changing [util.smartptr.atomic.weak]/15 to "Two weak_ptr objects are equivalent if they are both empty, or if they share ownership and store the same pointer value." In practice, an implementation of atomic<weak_ptr> will CAS on both the ownership (control block pointer) and stored pointer value, so it seems cleaner to pin down the stored pointer value of an empty weak_ptr.

History
Date User Action Args
2019-06-10 05:16:56adminsetmessages: + msg10430
2019-03-16 15:30:52adminsetmessages: + msg10356
2019-03-15 00:00:00admincreate