Proper way of transferring ownership of a std::vector< std::unique_ptr< int> > to a class being constructed

std::unique_ptr<T> is a non-copyable but movable type. Having a move-only type in a std:vector<T> make the std::vector<T> move-only, too. To have the compiler automatically move objects, you need to have an r-value for move-construction or move-assignment. Within your constructor the object vecOfIntPtrsOwnedByCaller is an l-value, although one which, despite its name, already owns the pointed … Read more

Why is std::make_unique not implemented using list initialization?

In C++20, this will compile: std::make_unique<point>(1, 2); due to the new rule allowing initializing aggregates from a parenthesized list of values. In C++17, you can just do: std::unique_ptr<point>(new point{1, 2}); That won’t work with make_shared though. So you can also just create a factory (forwarding left as an exercise): template <typename… Args> struct braced_init { … Read more

Initializing a std::unique_ptr by passing the address of the pointer

Somewhere under the covers, unique_ptr<foo> has a data member of type foo*. However, it’s not legitimate for a user of the class to directly modify that data member. Doing so would not necessarily preserve the class invariants of unique_ptr, in particular it wouldn’t free the old pointer value (if any). In your special case you … Read more

Performance of resizing std::vector

The claim that unique_ptr performs as well as a raw pointer after optimization mostly applies only to the basic operations on a single pointer, such as creation, dereferencing, assignment of a single pointer and deletion. Those operations are defined simply enough that an optimizing compiler can usually make the required transformations such that the resulting … Read more

Using a std::unordered_set of std::unique_ptr

You can also use a deleter that optionally doesn’t do anything. template<class T> struct maybe_deleter{ bool _delete; explicit maybe_deleter(bool doit = true) : _delete(doit){} void operator()(T* p) const{ if(_delete) delete p; } }; template<class T> using set_unique_ptr = std::unique_ptr<T, maybe_deleter<T>>; template<class T> set_unique_ptr<T> make_find_ptr(T* raw){ return set_unique_ptr<T>(raw, maybe_deleter<T>(false)); } // … int* raw = new … Read more

Why is “error: invalid application of ‘sizeof’ to an incomplete type using unique_ptr” fixed by adding an empty destructor? [duplicate]

If we go to the cppreference document for std::unique_ptr: std::unique_ptr may be constructed for an incomplete type T, such as to facilitate the use as a handle in the Pimpl idiom. If the default deleter is used, T must be complete at the point in code where the deleter is invoked, which happens in the … Read more

Why can you return a std::unique_ptr without std::move?

is there some other clause in the language specification that this exploits? Yes, see 12.8 §34 and §35: When certain criteria are met, an implementation is allowed to omit the copy/move construction of a class object […] This elision of copy/move operations, called copy elision, is permitted […] in a return statement in a function … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)