Skip to content

Proposal: Enable std::shared_ptr to delete objects with protected/private destructors via friendship #7873

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
davitkalantaryan opened this issue May 13, 2025 · 2 comments

Comments

@davitkalantaryan
Copy link

Hi all,

I’ve prepared a proposal to improve std::shared_ptr so that it can delete objects with protected or private destructors — provided that the class declares friend class std::shared_ptr<T>. Currently, this does not work, because the actual deletion is handled by internal helpers or lambdas that are not considered friends, even if shared_ptr<T> itself is.

This limitation weakens encapsulation and safety patterns. In a real project, I experienced this firsthand: forgetting that a particular object was managed by shared_ptr, I deleted it manually — which led to hard-to-trace crashes. Declaring the destructor as protected was meant to prevent such mistakes, but it didn’t help, because shared_ptr was unable to perform deletion due to access restrictions.


What This Proposal Suggests

  • If shared_ptr<T> is declared a friend of T, it should be allowed to delete T directly — not via an inaccessible internal deleter.
  • This change enables safer ownership enforcement without requiring boilerplate custom deleters.
  • (Optional) Introduce a mechanism to observe reference count changes via a callback, useful for debugging and UI synchronization.

References


@jwakely — since you've contributed significantly to shared_ptr and libstdc++, I would be very grateful for your input on this topic. If you find the idea worthwhile, I’d also greatly appreciate your consideration as a potential sponsor for WG21 discussion. Of course, I welcome feedback from all reviewers as well.

Thank you in advance!
— Davit Kalantaryan


Appendix: Clarification After Community Feedback

Following early discussions (particularly on Reddit), I’ve added this clarification to the proposal:

Many developers rightly point out that if a smart pointer manages an object’s lifetime, manual deletion should never occur. While that’s true in principle, large and evolving codebases make mistakes inevitable. That’s why C++ provides access specifiers like private and protected: not because developers are careless, but because the compiler helps enforce correct usage.

Saying “just don’t delete manually” is similar to saying “we don’t need private or protected; developers should just avoid calling the wrong function.” But we do have those tools — to make incorrect usage harder, not merely discouraged.

This proposal aims to bring the same level of safety to destructor access. If shared_ptr<T> is a friend, it should be allowed to delete T, just like any other friend.

@jensmaurer
Copy link
Member

This issue tracker is for editorial issues related to the C++ standard only; see https://github.com/cplusplus/draft/wiki/How-to-tell-if-an-issue-is-editorial .

This write-up appears to present a proposal, not an editorial issue; please see https://isocpp.org/std/submit-a-proposal for hints on how to submit these.

@jensmaurer jensmaurer closed this as not planned Won't fix, can't repro, duplicate, stale May 13, 2025
@jwakely
Copy link
Member

jwakely commented May 14, 2025

I'm pretty sure you can already do this by using std:: allocate_shared and making the allocator a friend.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants