push_back or emplace_back with std::make_unique

It doesn’t make a difference as far as construction of the new object is concerned; you already have a unique_ptr<Foo> prvalue (the result of the call to make_unique) so both push_back and emplace_back will call the unique_ptr move constructor when constructing the element to be appended to the vector. If your use case involves accessing … Read more

When moving a unique_ptr into a lambda, why is it not possible to call reset?

Why does this happen? Because the function-call operator of a lambda, Unless the keyword mutable was used in the lambda-expression, the function-call operator is const-qualified and the objects that were captured by copy are non-modifiable from inside this operator(). and Is it possible to capture the std::unique_ptr in another way which allows to call reset() … Read more

How to implement make_unique function in C++11? [duplicate]

Copied from make_unique and perfect forwarding (the same is given in Herb Sutter’s blog) template<typename T, typename… Args> std::unique_ptr<T> make_unique(Args&&… args) { return std::unique_ptr<T>(new T(std::forward<Args>(args)…)); } If you need it in VC2012, see Is there a way to write make_unique() in VS2012? Nevertheless, if the solution in sasha.sochka’s answer compiles with your compiler, I would … Read more

How to make std::make_unique a friend of my class

make_unique perfect forwards the arguments you pass to it; in your example you’re passing an lvalue (x) to the function, so it’ll deduce the argument type as int&. Your friend function declaration needs to be friend std::unique_ptr<A> std::make_unique<A>(T&); Similarly, if you were to move(x) within CreateA, the friend declaration would need to be friend std::unique_ptr<A> … Read more

Calling initializer_list constructor via make_unique/make_shared

std::make_unique is a function template which deduces the argument types which are passed to the object constructor. Unfortunately, braced lists are not deducible (with an exception for auto declarations), and so you cannot instantiate the function template when that missing parameter type. You can either not use std::make_unique, but please don’t go that route – … Read more

Can a unique_ptr take a nullptr value?

It will work. From Paragraphs 20.7.1.2.3/8-9 of the C++11 Standard about the unique_ptr<> class template: unique_ptr& operator=(nullptr_t) noexcept; Effects: reset(). Postcondition: get() == nullptr This means that the definition of class template unique_ptr<> includes an overload of operator = that accepts a value of type nullptr_t (such as nullptr) as its right hand side; the … Read more

What happens to unique_ptr after std::move()?

No, you cannot do that instead. Moving the unique_ptr nulls it. If it didn’t, then it would not be unique. I am of course assuming that attachActor doesn’t do something silly like this: attachActor(std::unique_ptr<Tony>&&) { // take the unique_ptr by r-value reference, // and then don’t move from it, leaving the // original intact } … Read more