deductiongu...::shared_ptr(3)
π NAME π
deductionguidesforstd::shared_ptr β deductionguidesforstd::shared_ptr
Okay so first things first, bestie: this page is deductiongu...::shared_ptr(3), and it lives in the C++ Standard Libary corner of the universe. The name line is literally the same thing twice, and honestly? Iconic.
β¨ Synopsis β¨
Defined in header <memory>:
template< class T > (1) (since C++17)
shared_ptr( std::weak_ptr<T> ) -> shared_ptr<T>;
template< class T, class D > (2) (since C++17)
shared_ptr( std::unique_ptr<T, D> ) -> shared_ptr<T>;
These deduction guides are here for std::shared_ptr to account for the edge cases missed by the implicit deduction guides. Thatβs the whole reason they exist, no cap.
π¨ Important vibe check: 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. So donβt try to make fetch happen there.
Both guides are since C++17:
- (1) is the
std::weak_ptr<T>one:shared_ptr( std::weak_ptr<T> ) -> shared_ptr<T>; - (2) is the
std::unique_ptr<T, D>one:shared_ptr( std::unique_ptr<T, D> ) -> shared_ptr<T>;
π§ͺ Example π§ͺ
Run this code π
// Run this code
#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
}
The comments are doing the lore drop: both std::weak_ptr w{p}; and std::shared_ptr p2{w}; are using the explicit deduction guide in this case. Slay.
Thatβs the tea π« β served fresh from http://cppreference.com, dated 2024.06.10, and still answering to deductiongu...::shared_ptr(3). Mwah πβ¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms