top of page

Smart pointers for overwrite in C++20

Have you ever wondered why we got make_shared_for_overwrite and make_unique_for_overwrite in C++20? Understanding the significance of these new functions and how they differ from the familiar std::make_shared and std::make_unique introduced in C++11 can greatly enhance your coding practices.


The Power of std::make_shared and std::make_unique

Before diving into the advancements in C++20, let's briefly revisit the advantages of std::make_shared and std::make_unique:

  • Efficiency and Safety: These factory functions create instances of std::shared_ptr and std::unique_ptr efficiently. They allocate the memory for the managed object and the control block in a single allocation, reducing overhead and improving cache locality.

  • Exception Safety: By using std::make_shared and std::make_unique, you avoid potential resource leaks caused by exceptions during the separate allocation of the control block and the managed object.


Advancements in C++20: make_shared_for_overwrite and make_unique_for_overwrite

C++20 builds on these foundations with features that offer more control and flexibility when overwriting existing managed objects:

Performance Improvement: By avoiding the overhead associated with memory reallocation, these new functions can improve performance in applications where objects are frequently overwritten.


In the next video, I will delve deeper into the practical applications of these features, demonstrating how and where they should be used. By mastering these new tools, you can write more efficient, safe, and maintainable C++ code.

Let's keep learning together and enhance our C++ skills!




Recent Posts

See All

Virtual Constexpr Functions

In the latest episode of the C++Next  series, we explore the fascinating world of constexpr polymorphism . This episode is packed with...

bottom of page