folly::to_weak_ptr

to_weak_ptr

Synopsis

Declared in <folly/Memory.h>

template<typename T>
std::weak_ptr<T>
to_weak_ptr(std::shared_ptr<T> const& ptr);

Description

Make a weak_ptr and return it from a shared_ptr without specifying the template type parameter and letting the compiler deduce it.

So you can write this:

auto wptr = to_weak_ptr(getSomethingShared<T>());

Instead of this:

auto wptr = weak_ptr<T>(getSomethingShared<T>());

Useful when T is long, such as:

using T = foobar::FooBarAsyncClient;

Return Value

a weak_ptr observing ptr

Parameters

NameDescription
ptrthe shared_ptr to observe