0

Where make_unique is better than unique_ptr(raw pointer)

Hi If both (unique_ptr and make_unique) have same output like i have in this code, what's the advantage of make_unique. https://sololearn.com/compiler-playground/cnxp9hb8AB1E/?ref=app

30th Jul 2025, 6:29 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
4 odpowiedzi
+ 2
make_unique is a fundamental improvement for modern C++ development and it provides: Crucial exception safety guarantees. More concise and readable code. Reduced redundancy. Potential minor performance benefits.
30th Jul 2025, 9:02 PM
BroFar
BroFar - avatar
+ 1
reading a reddit topic similar to this question gave me this link as to why make_unique was added later than make_shared: https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3588.htm
30th Jul 2025, 11:38 PM
Bob_Li
Bob_Li - avatar
31st Jul 2025, 12:01 AM
BroFar
BroFar - avatar
0
This all points look good @BroFar but details would help a lot. It provides safety guarantee , but when and which case? can we demonstrate it with failure for unique_ptr(raw pointer) constructor and good for make_unique? What redundancy is removed by make unique? Which performance benefit from make_unique? I am aware that make_shared allocate heap memory once making it effective, but not sure about performance gain of make_unique
31st Jul 2025, 3:06 AM
Ketan Lalcheta
Ketan Lalcheta - avatar