- memory[meta header]
- std[meta namespace]
- enable_shared_from_this[meta class]
- function[meta id-type]
- cpp17[meta cpp]
weak_ptr<T> weak_from_this() noexcept;
weak_ptr<const T> weak_from_this() const noexcept;- weak_ptr[link /reference/memory/weak_ptr.md]
thisポインタをweak_ptrに変換する。
*thisのインスタンスがshared_ptrオブジェクトとして共有されていること。
thisポインタを指すweak_ptrオブジェクトを返す。
#include <cassert>
#include <memory>
struct X : public std::enable_shared_from_this<X> {
std::weak_ptr<X> f()
{
// thisを指すweak_ptrオブジェクトを作る
return weak_from_this();
}
};
int main()
{
std::shared_ptr<X> p(new X());
std::weak_ptr<X> q = p->f();
assert(p == q.lock());
}- std::weak_ptr[link /reference/memory/weak_ptr.md]
- q.lock()[link /reference/memory/weak_ptr/lock.md]
- C++17
- GCC: 7.3
- Clang: 3.9
- Visual C++: ??