Skip to main content

Deduction guides for std::shared_ptr

C++17
template< class T >
shared_ptr( std::weak_ptr<T> ) -> shared_ptr<T>;

template< class T, class D >
shared_ptr( std::unique_ptr<T, D> ) -> shared_ptr<T>;

These deduction guides are provided for std::shared_ptr to account for the edge cases missed by the implicit deduction guides.

Note that there is no class template argument deduction from pointer types because it is impossible to distinguish pointers obtained from array and non-array forms of new.

Example

include <memory>

int main()
{
auto p = std::make_shared<int>(42);
std::weak_ptr w{p}; // explicit deduction guide is used in this case
std::shared_ptr p2{w}; // explicit deduction guide is used in this case
}

Deduction guides for std::shared_ptr

C++17
template< class T >
shared_ptr( std::weak_ptr<T> ) -> shared_ptr<T>;

template< class T, class D >
shared_ptr( std::unique_ptr<T, D> ) -> shared_ptr<T>;

These deduction guides are provided for std::shared_ptr to account for the edge cases missed by the implicit deduction guides.

Note that there is no class template argument deduction from pointer types because it is impossible to distinguish pointers obtained from array and non-array forms of new.

Example

include <memory>

int main()
{
auto p = std::make_shared<int>(42);
std::weak_ptr w{p}; // explicit deduction guide is used in this case
std::shared_ptr p2{w}; // explicit deduction guide is used in this case
}