Home > Backend Development > C++ > body text

Why Doesn't `std::shared_ptr` Have a Specialization for Array Deletion?

Linda Hamilton
Release: 2024-11-12 21:22:02
Original
453 people have browsed it

Why Doesn't `std::shared_ptr` Have a Specialization for Array Deletion?

Why std::shared_ptr Was Left Unspecialized

In contrast to std::unique_ptr, which appropriately invokes delete[] in its destructor for arrays, std::shared_ptr lacks a comparable specialization. Consequently, users must manually provide a deleter that correctly deallocates arrays.

Reasons for the Oversight

While the absence of a std::shared_ptr specialization may appear to be an oversight, it resulted from several factors:

  • Lack of Controversy: Unlike std::copy_if, which sparked controversy, std::shared_ptr's absence did not generate significant debate.
  • Missed Opportunity: Despite discussions within the C committee, no formal proposal was submitted to address the need for array support in std::shared_ptr.

Recent Developments

Renewed interest in array support for std::shared_ptr has emerged, leading to the creation of a draft technical specification (TS). This TS (n4077.html) proposes a mechanism to extend shared_ptr's functionality to arrays.

Current Status and Future Prospects

The draft TS has been approved as part of C 17, providing official support for array handling in std::shared_ptr. This feature enhancement enables seamless array management and simplifies memory allocation tasks for programmers.

The above is the detailed content of Why Doesn't `std::shared_ptr` Have a Specialization for Array Deletion?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template