I've got a simple noob question that I can't find an answer to:
In C++ how do you convert a regular object
int i;
into a std::unique_ptr
?
std::unique_ptr<int> iptr = &i; //invalid
std::unique_ptr<int> iptr = static_cast<std::unique_ptr<int>>(&i); //invalid
Thanks.
Afterword. The flawless conversion of an std::unique_ptr to a compatible std::shared_ptr makes it possible to write efficient and safe factory functions. However, note that an std::shared_ptr cannot be converted to an std::unique_ptr.
std::unique_ptr is a smart pointer that owns and manages another object through a pointer and disposes of that object when the unique_ptr goes out of scope. The object is disposed of, using the associated deleter when either of the following happens: the managing unique_ptr object is destroyed.
A unique_ptr does not share its pointer. It cannot be copied to another unique_ptr , passed by value to a function, or used in any C++ Standard Library algorithm that requires copies to be made. A unique_ptr can only be moved.
In short: Use unique_ptr when you want a single pointer to an object that will be reclaimed when that single pointer is destroyed. Use shared_ptr when you want multiple pointers to the same resource.
You don't. That object cannot be deleted by delete
, which is what the unique_ptr
is going to do. You need
auto iptr = make_unique<int>();
Here, we define make_unique as a utility function identical to make_shared, which should have been Standard but unfortunately was overlooked. Here's the implementation in brief:
template<typename T, typename... Args> std::unique_ptr<T> make_unique(Args&&... args) {
return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
}
You don't. i
was not dynamically allocated so it doesn't need to be deleted. If you wrapped a smart pointer around its address, it would do delete &i
at some point and give you undefined behaviour. You should only wrap something you have new
ed in a smart pointer, like so:
std::unique_ptr<int> ptr(new int(5));
The whole point of a smart pointer is that it manages the lifetime of a dynamically allocated object for you. i
has automatic storage duration so will be destroyed at the end of its scope. You don't need anything to help you with that.
I believe this will work:
int i;
auto deleter = [](int *ptr){};
std::unique_ptr<int, decltype(deleter)> iptr(&i, deleter);
You have to provide a custom deleter that does nothing. The default deleter cannot delete an automatic variable not allocated by new
. (However, this defeats the purpose of using a smart pointer, but shows that it is possible).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With