Examining the Case for a Non-Atomic Equivalent of std::shared_ptr
Introduction
Smart pointers in C 11 offer a powerful mechanism for memory management, with std::shared_ptr being the primary choice for shared ownership. However, its atomic nature introduces an overhead that may be unnecessary or undesirable in certain scenarios. This article explores the absence of a non-atomic equivalent of std::shared_ptr in the standard library and examines potential reasons behind this decision.
The Search for a Non-Atomic Alternative
Contrary to the premise of the question, std::shared_ptr is not the only smart pointer in
Reasons for the Exclusion
Despite the potential benefits of a non-atomic shared_ptr, the C standards committee opted not to include one in C 11. The decision stemmed from a heated debate at the 2010 Rapperswil meeting. Arguments against its inclusion included:
Conclusion
The absence of a non-atomic equivalent of std::shared_ptr in the C 11 standard is multifaceted. While it compromises the "pay for what you use" principle, it prioritizes consistency, error avoidance, and minimal performance overhead. The decision reflects a compromise between providing optimal flexibility and ensuring code robustness.
The above is the detailed content of Why is there no non-atomic equivalent of std::shared_ptr in the C standard library?. For more information, please follow other related articles on the PHP Chinese website!