Title
Interaction between make_shared and enable_shared_from_this is underspecified
Status
c++17
Section
[util.smartptr.shared.create]
Submitter
Joe Gottman

Created on 2016-04-02.00:00:00 last changed 90 months ago

Messages

Date: 2016-08-02.17:19:11

Proposed resolution:

This wording is relative to N4582.

  1. Change [util.smartptr.shared.create] indicated:

    template<class T, class... Args> shared_ptr<T> make_shared(Args&&... args);
    template<class T, class A, class... Args>
      shared_ptr<T> allocate_shared(const A& a, Args&&... args);
    

    […]

    -6- Remarks: The shared_ptr constructor called by this function enables shared_from_this with the address of the newly constructed object of type T. Implementations should perform no more than one memory allocation. [Note: This provides efficiency equivalent to an intrusive smart pointer. — end note]

Date: 2016-08-02.17:19:11

[ 2016-08 Chicago ]

Monday PM: Move to Tentatively Ready

Date: 2017-02-02.00:41:18

[ 2016-05 Issues Telecon ]

Jonathan Wakely to provide updated wording.

Date: 2016-04-02.00:00:00

For each public constructor of std::shared_ptr, the standard says that constructor enables shared_from_this if that constructor is expected to initialize the internal weak_ptr of a contained enable_shared_from_this<X> object. But there are other ways to construct a shared_ptr than by using a public constructor. The template functions make_shared and allocate_shared both require calling a private constructor, since no public constructor can fulfill the requirement that at most one allocation is made. The standard does not specify that that private constructor enables shared_from_this; therefore in the following code:

struct Foo : public std::enable_shared_from_this<Foo> {};

int main() {
  auto p = std::make_shared<Foo>();
  assert(p == p->shared_from_this());
  return 0;
}

it is unspecified whether the assertion will fail.

History
Date User Action Args
2017-07-30 20:15:43adminsetstatus: wp -> c++17
2016-11-14 03:59:28adminsetstatus: pending -> wp
2016-11-14 03:55:22adminsetstatus: ready -> pending
2016-08-02 17:19:11adminsetmessages: + msg8333
2016-08-02 17:19:11adminsetstatus: new -> ready
2016-05-22 15:38:38adminsetmessages: + msg8140
2016-05-07 20:27:58adminsetmessages: + msg8090
2016-04-02 00:00:00admincreate