Title
Contradiction in empty shared_ptr
Status
c++11
Section
[util.smartptr.shared.obs]
Submitter
Peter Dimov

Created on 2007-08-24.00:00:00 last changed 162 months ago

Messages

Date: 2010-10-21.18:28:33

Proposed resolution:

In keeping the N2351 spirit and obviously my preference, change [util.smartptr.shared.obs]:

T* get() const;

Returns: the stored pointer. Returns a null pointer if *this is empty.

Date: 2010-10-21.18:28:33

[ 2009-07 Frankfurt: ]

We favor option 1, move to Ready.

Howard: Option 2 commented out for clarity, and can be brought back.
Date: 2010-10-21.18:28:33

[ Sophia Antipolis: ]

We heard from Peter Dimov, who explained his reason for preferring solution 1.

Because it doesn't seem to add anything. It simply makes the behavior for p = 0 undefined. For programmers who don't create empty pointers with p = 0, there is no difference. Those who do insist on creating them presumably have a good reason, and it costs nothing for us to define the behavior in this case.

The aliasing constructor is sharp enough as it is, so "protecting" users doesn't make much sense in this particular case.

> Do you have a use case for r being empty and r being non-null?

I have received a few requests for it from "performance-conscious" people (you should be familiar with this mindset) who don't like the overhead of allocating and maintaining a control block when a null deleter is used to approximate a raw pointer. It is obviously an "at your own risk", low-level feature; essentially a raw pointer behind a shared_ptr facade.

We could not agree upon a resolution to the issue; some of us thought that Peter's description above is supporting an undesirable behavior.

Date: 2010-10-21.18:28:33

[ Bellevue: ]

Adopt option 1 and move to review, not ready.

There was a lot of confusion about what an empty shared_ptr is (the term isn't defined anywhere), and whether we have a good mental model for how one behaves. We think it might be possible to deduce what the definition should be, but the words just aren't there. We need to open an issue on the use of this undefined term. (The resolution of that issue might affect the resolution of issue 711.)

The LWG is getting more uncomfortable with the aliasing proposal (N2351) now that we realize some of its implications, and we need to keep an eye on it, but there isn't support for removing this feature at this time.

Date: 2007-08-24.00:00:00

A discussion on comp.std.c++ has identified a contradiction in the shared_ptr specification. The note:

[ Note: this constructor allows creation of an empty shared_ptr instance with a non-NULL stored pointer. -end note ]

after the aliasing constructor

template<class Y> shared_ptr(shared_ptr<Y> const& r, T *p);

reflects the intent of N2351 to, well, allow the creation of an empty shared_ptr with a non-NULL stored pointer.

This is contradicted by the second sentence in the Returns clause of [util.smartptr.shared.obs]:

T* get() const;

Returns: the stored pointer. Returns a null pointer if *this is empty.

History
Date User Action Args
2011-08-23 20:07:26adminsetstatus: wp -> c++11
2010-10-21 18:28:33adminsetmessages: + msg3513
2010-10-21 18:28:33adminsetmessages: + msg3512
2010-10-21 18:28:33adminsetmessages: + msg3511
2010-10-21 18:28:33adminsetmessages: + msg3510
2007-08-24 00:00:00admincreate