11

C++20 introduces new function std::make_shared_for_overwrite() in addition to std::make_shared(): https://en.cppreference.com/w/cpp/memory/shared_ptr/make_shared

Why old make_shared was not enough and in what situation one needs to use new function?

2
  • 4
    "Same as (2), except that the individual array elements are default-initialized." Commented Jun 14, 2021 at 9:47
  • 2
    So looks rather like a convenience function (C analogy: malloc vs. calloc). Commented Jun 14, 2021 at 10:03

1 Answer 1

24

std::make_shared() value initialises the object(s) it creates, which might be an unnecessary step if you intend to assign values over them later.

std::make_shared_for_overwrite() default initialises the object(s) it creates.

The difference only matters for (sub-)objects of fundamental types, where there is no initialiser.

std::make_shared<int[1000][1000]>() will allocate and zero a million ints std::make_shared_for_overwrite<int[1000][1000]>() will allocate a million ints

Sign up to request clarification or add additional context in comments.

1 Comment

"The difference only matters for fundamental types, and arrays thereof." Or structs thereof. Or any type that's trivially default constructible.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.