Converting `std::unique_ptr` to `std::shared_ptr`

When working with C++, smart pointers offer a great way to manage the lifetime of dynamically allocated objects. The standard library provides two popular smart pointer types: std::unique_ptr and std::shared_ptr.

While std::unique_ptr is used for exclusive ownership of a resource, there are situations where you may need to convert it to a std::shared_ptr to facilitate shared ownership. In this blog post, we will explore different ways to achieve this conversion.

Converting using std::shared_ptr constructor

One straightforward way to convert a std::unique_ptr to a std::shared_ptr is by using the constructor of std::shared_ptr. The constructor takes a std::unique_ptr as its argument and transfers ownership to the std::shared_ptr.

Here’s an example:

std::unique_ptr<int> uniquePtr(new int(42));
std::shared_ptr<int> sharedPtr(std::move(uniquePtr)); // Conversion using std::shared_ptr constructor

In the example above, we create a std::unique_ptr to an int with a value of 42. Then, we convert it to a std::shared_ptr by passing the std::unique_ptr as an argument to the std::shared_ptr constructor.

Converting using std::shared_ptr::reset()

Another way to convert a std::unique_ptr to a std::shared_ptr is by resetting the std::unique_ptr and assigning its value to a std::shared_ptr.

std::unique_ptr<int> uniquePtr(new int(42));
std::shared_ptr<int> sharedPtr;

sharedPtr.reset(uniquePtr.release()); // Conversion using std::shared_ptr::reset()

In the code snippet above, we first create a std::unique_ptr to an int and assign a value of 42. Then, we reset the std::unique_ptr using release(), which transfers ownership to our std::shared_ptr.

Conclusion

Converting a std::unique_ptr to a std::shared_ptr is a useful technique when you need to share ownership of a resource in C++. By using either the std::shared_ptr constructor or the std::shared_ptr::reset() function, you can easily achieve this conversion.

Remember, when converting, it’s essential to consider the ownership semantics and ensure that it aligns with your intended usage of the resource.

#SmartPointers #C++