Search code examples
c++c++11c++14shared-ptr

Are weak pointers guaranteed to have expired by the time the std::shared_ptr deleter runs?


If I have a std::shared_ptr<Foo> with a custom deleter, is it guaranteed that all associated weak pointers are seen as expired by the deleter? (I would appreciate it very much if you could cite relevant sections in the standard.)

In other words is the assertion below guaranteed not to fire?

std::weak_ptr<Foo> weak;
std::shared_ptr<Foo> strong{
  new Foo,
  [&weak] (Foo* f) {
    assert(weak.expired());
    delete f;
  },
};

weak = strong;
strong.reset();

Solution

  • There is apparently nothing in the C++14 standard that guarantees this. I've now opened a defect report for the standard covering the problem.