Difference between these two?

What is the difference between these two pieces of code? Are they the same with just a different way of defining?

 
  std::unique_ptr<int> y(new int(20));


and

 
  std::unique_ptr<int> y = std::make_unique<int>(20);


When would you use make_unique over the first example? I'm trying to wrap my head around smart pointers. I've used them plenty, but don't actually understand what they're doing. I guess you could say I've gotten lucky. But I really need to understand this stuff better.
> Are they the same with just a different way of defining?

Yes.


> When would you use make_unique over the first example?

Always. See: 'Motivation And Scope' in http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3588.txt

That "unspecified-evaluation-order leak" in JLBorges link is mildly surprising. Basically, since both news can execute before either unique_ptr ctor in the following code, if the first-executed ctor throws, the other new leaks. make_unique prevents this since it does the new iteself.
 
foo(unique_ptr<X>(new X), unique_ptr<Y>(new Y))

Topic archived. No new replies allowed.