2014-03-21 23:20:28 8 Comments

Does std::make_unique have any efficiency benefits like std::makes_shared?

Compared to manually constructing std::unique_ptr:

std::make_unique<int>(1);         // vs
std::unique_ptr<int>(new int(1));


@Volodymyr Lashko 2017-07-20 09:16:18

A reason why you would have to use std::unique_ptr(new A()) or std::shared_ptr(new A()) directly instead of std::make_*() is being unable to access the constructor of class A outside of current scope.

@bames53 2014-03-21 23:31:48

The motivation behind make_unique is primarily two-fold:

  • make_unique is safe for creating temporaries, whereas with explicit use of new you have to remember the rule about not using unnamed temporaries.

    foo(make_unique<T>(), make_unique<U>()); // exception safe
    foo(unique_ptr<T>(new T()), unique_ptr<U>(new U())); // unsafe*
  • The addition of make_unique finally means we can tell people to 'never' use new rather than the previous rule to "'never' use new except when you make a unique_ptr".

There's also a third reason:

  • make_unique does not require redundant type usage. unique_ptr<T>(new T()) -> make_unique<T>()

None of the reasons involve improving runtime efficiency the way using make_shared does (due to avoiding a second allocation, at the cost of potentially higher peak memory usage).

* It is expected that C++17 will include a rule change that means that this is no longer unsafe. See C++ committee papers P0400R0 and P0145R3.

@Timothy Shields 2014-03-21 23:32:36

It would make more sense to say std::unique_ptr and std::shared_ptr are why we can tell people to "never use new."

@bames53 2014-03-21 23:34:27

@TimothyShields Yeah, that's what I mean. It's just that in C++11 we have make_shared and so make_unique is the final piece that was previously missing.

@Dan Nissenbaum 2016-03-03 14:23:41

Any way you could mention briefly, or link to, the reason for not using unnamed temporaries?

@Dan Nissenbaum 2016-03-03 14:35:42

Actually, from, I've got it... From that answer, consider the following function call f: f(unique_ptr<T>(new T), function_that_can_throw()); - to quote the answer: The compiler is allowed to call (in order): new T, function_that_can_throw(), unique_ptr<T>(...). Obviously if function_that_can_throw actually throws then you leak. make_unique prevents this case. So, my question is answered.

@Daniel 2016-08-18 22:04:25

@bames53 Unfortunately there is still need to use raw new if you want to pass a custom facet to std::locale (overload 7).

@tambre 2017-04-21 07:02:49

@bames53 Have there been any news regarding the footnote?

@bames53 2017-04-21 12:42:39

@tambre The linked papers are part of the C++17 draft, and there shouldn't be any significant changes between now and publication later this year. Clang already implements these rules. It's not clear from gcc's status page if they do or not yet.

@Passer By 2017-07-05 17:25:39

Took a really long time to see why it is now safe, but yeah it is. BTW thanks for updating your answer

@Timothy Shields 2014-03-21 23:25:22

std::make_unique and std::make_shared are there for two reasons:

  1. So that you don't have to explicitly list the template type arguments.
  2. Additional exception safety over using std::unique_ptr or std::shared_ptr constructors. (See the Notes section here.)

It's not really about runtime efficiency. There is the bit about the control block and the T being allocated all at once, but I think that's more a bonus and less a motivation for these functions to exist.

@0x499602D2 2014-03-21 23:26:56

They're also there for exception-safety.

@Timothy Shields 2014-03-21 23:27:33

@0x499602D2 And that, good addition. This page talks about that.

Related Questions

Sponsored Content

31 Answered Questions

12 Answered Questions

[SOLVED] What exactly is nullptr?

27 Answered Questions

18 Answered Questions

[SOLVED] Why should C++ programmers minimize use of 'new'?

35 Answered Questions

35 Answered Questions

[SOLVED] Why is "using namespace std" considered bad practice?

6 Answered Questions

[SOLVED] Raw pointer lookup for sets of unique_ptrs

4 Answered Questions

[SOLVED] What is the difference between 'typedef' and 'using' in C++11?

2 Answered Questions

[SOLVED] std::make_unique for GDI+ objects

  • 2016-06-26 11:40:35
  • The Coder
  • 276 View
  • 2 Score
  • 2 Answer
  • Tags:   c++ c++11 c++14 gdi+

4 Answered Questions

[SOLVED] std::auto_ptr to std::unique_ptr

Sponsored Content