Home >Backend Development >C++ >Why isn't there a `std::shared_ptr` in the C standard library?

Why isn't there a `std::shared_ptr` in the C standard library?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-11-10 04:07:53522browse

Why isn't there a `std::shared_ptr` in the C   standard library?

A Deeper Dive into the Absence of std::shared_ptr

In the C standard library, there exists a specialized version of std::unique_ptr for arrays, std::unique_ptr, that correctly invokes delete[] in its destructor. However, such a specialization is lacking for std::shared_ptr, necessitating the use of custom deleters like the one showcased in the question.

Rationale for the Lack of Specialization

The absence of std::shared_ptr stems from a combination of factors:

  • LWG Discussion: The Library Working Group (LWG) considered the idea but ultimately dismissed it due to controversy surrounding an additional feature proposed for shared_ptr.
  • Lack of Formal Proposal: Despite informal discussions, no official proposal has ever been submitted to the LWG for the creation of std::shared_ptr.

Current Status and Future Prospects

Despite the historical hesitation, recent discussions have reignited interest among LWG members. While a prototype has been implemented, a formal proposal remains outstanding. The feasibility and ultimate adoption of std::shared_ptr remains uncertain.

Upcoming Developments

An open-std.org draft TS (Technical Specification) proposes array support for std::shared_ptr. This TS, if adopted, would introduce std::shared_ptr as part of the C standard.

Update for C 17

The draft TS has since been incorporated into C 17, providing official support for std::shared_ptr. Refer to case 3 of shared_ptr::shared_ptr() for details.

The above is the detailed content of Why isn't there a `std::shared_ptr` in the C standard library?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn