跳到主要内容

std::weak_ptr 的推导指南

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

为 std::weak_ptr 提供了一个推导指南,以应对隐式推导指南所遗漏的边缘情况。

示例

#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::weak_ptr 的推导指南

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

为 std::weak_ptr 提供了一个推导指南,以应对隐式推导指南所遗漏的边缘情况。

示例

#include <memory>

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