The difference is that std::make_unique
returns an object of type std::unique_ptr
and new
returns a pointer to the created object. For memory allocation failures, they will both throw. Hold on, it's not that simple. Read further.
Consider such a function below:
void func(ClassA* a, ClassB* b){
......
}
When you make a call like func(new A(), new B())
; The compiler may choose to evaluate the function arguments from left to right, or in any order it so wishes. Let's assume left to right evaluation: What happens when the first new
expression succeeds but the second new
expression throws?
The real danger here is when you catch such exception; Yes, you may have caught the exception thrown by new B()
, and resume normal execution, but new A()
already succeeded, and its memory will be silently leaked. Nobody to clean it up... * sobs...
But with make_unique
, you cannot have a leak because, stack unwinding will happen ( and the destructor of the previously created object will run). Hence, having a preference for make_unique
will constrain you towards exception safety. In this case, std::make_unique
provides a "Basic Exception Safety" that the memory allocated and object created by new
will never be orphaned no matter what. Even till the ends of time... :-)
You should read Herb Sutter GoTW102
std::make_unique(SomeObject(...))
results in calling constructor and copy/move constructor. Definitely, a better choice is to callstd::make_unique
directly with construtor parameters of underlying templated type.