定义于头文件
template< class T > class enable_shared_from_this; (C++11 起)
std::enable_shared_from_this 能让其一个对象(假设其名为 t ,且已被一个 std::shared_ptr 对象 pt 管理)安全地生成其他额外的 std::shared_ptr 实例(假设名为 pt1, pt2, ... ) ,它们与 pt 共享对象 t 的所有权。
若一个类 T 继承 std::enable_shared_from_this ,则会为该类 T 提供成员函数: shared_from_this 。 当 T 类型对象 t 被一个为名为 pt 的 std::shared_ptrT::shared_from_this 成员函数,将会返回一个新的 std::shared_ptrpt 共享 t 的所有权。
std::enable_shared_from_this<T>::enable_shared_from_this
| constexpr enable_shared_from_this() noexcept; | (1) | (C++11 起) |
| enable_shared_from_this( const enable_shared_from_this | (2) | (C++11 起) |
构造新的 enable_shared_from_this 对象。值初始化私有 std::weak_ptr
| obj | - | 要复制的 enable_shared_from_this |
无移动构造函数:从导出自 shared_from_this 的对象移动不转移其共享身份。
- #include <iostream>
- #include <memory>
-
- struct Foo : public std::enable_shared_from_this<Foo>
- {
- Foo() {} // 隐式调用 enable_shared_from_this 构造函数
- std::shared_ptr<Foo> getFoo()
- {
- return shared_from_this();
- }
- };
-
- int main()
- {
- std::shared_ptr<Foo> pf1(new Foo);
- std::shared_ptr<Foo> pf2 = pf1->getFoo(); // 与 pf1 共享对象所有权
- std::cout << "pf2.use_count() " << pf2.use_count() << std::endl;
-
- {
- pf2.reset();
- std::cout << "pf2.use_count() " << pf2.use_count() << std::endl;
- }
- }

std::enable_shared_from_this<T>::~enable_shared_from_this
~enable_shared_from_this();
销毁 *this 。
std::enable_shared_from_this<T>::operator=
-
- enable_shared_from_this<T>& operator=
- ( const enable_shared_from_this<T> &obj ) noexcept;(C++11 起)
不做任何事;返回 *this 。
| obj | - | 赋值给 *this 的 enable_shared_from_this |
*this
私有的 std::weak_ptr
注意:定义 enable_shared_from_this::operator= 为受保护以避免意外切片,但允许导出类拥有默认的赋值运算符。
- #include <memory>
- #include <iostream>
-
- class SharedInt : public std::enable_shared_from_this<SharedInt>
- {
- public:
- explicit SharedInt(int n) : mNumber(n) {}
- SharedInt(const SharedInt&) = default;
- SharedInt(SharedInt&&) = default;
- ~SharedInt() = default;
-
- // 两个赋值运算符都使用 enable_shared_from_this::operator=
- SharedInt& operator=(const SharedInt&) = default;
- SharedInt& operator=(SharedInt&&) = default;
-
- int number() const
- {
- return mNumber;
- }
-
- private:
- int mNumber;
- };
-
- int main()
- {
- std::shared_ptr<SharedInt> a = std::make_shared<SharedInt>(2);
- std::shared_ptr<SharedInt> b = std::make_shared<SharedInt>(4);
- *a = *b;
- std::cout << a->number() << std::endl;
- }
std::enable_shared_from_this<T>::shared_from_this
| shared_ptr | (1) | |
| shared_ptr | (2) |
返回与所有指代 *this 的 std::shared_ptr 共享 *this 所有权的 std::shared_ptr
等效地执行 std::shared_ptrweak_this 是 enable_shared_from_this 的私有 mutable std::weak_ptr
只容许在先前共享的对象,即 std::shared_ptr 所管理的对象上调用 shared_from_this 。(特别是不能在构造 *this 期间 shared_from_this 。)
否则行为未定义 (C++17 前)抛出 std::bad_weak_ptr (由参数为默认构造的 weak_this 的 shared_ptr 构造函数) (C++17 起)。
与之前存在的 std::shared_ptr 共享 *this 所有权的 std::shared_ptr
- #include <iostream>
- #include <memory>
-
- struct Foo : public std::enable_shared_from_this<Foo>
- {
- Foo()
- {
- std::cout << "Foo::Foo\n";
- }
- ~Foo()
- {
- std::cout << "Foo::~Foo\n";
- }
- std::shared_ptr<Foo> getFoo()
- {
- return shared_from_this();
- }
- };
-
- int main()
- {
- Foo *f = new Foo;
- std::shared_ptr<Foo> pf1;
-
- {
- std::shared_ptr<Foo> pf2(f);
- pf1 = pf2->getFoo(); // 与 pf2 的对象共享所有权
- }
-
- std::cout << "pf2 is gone\n";
- }
enable_shared_from_this 的常见实现为:其内部保存着一个对 this 的弱引用(例如 std::weak_ptr )。 std::shared_ptr 的构造函数检测无歧义且可访问的 (C++17 起) enable_shared_from_this 基类,并且若内部存储的弱引用未为生存的 std::shared_ptr 占有,则 (C++17 起)赋值新建的 std::shared_ptr 为内部存储的弱引用。为已为另一 std::shared_ptr 所管理的对象构造一个 std::shared_ptr ,将不会考虑内部存储的弱引用,从而将导致未定义行为。
只允许在先前已被std::shared_ptr 管理的对象上调用 shared_from_this 。否则调用行为未定义 (C++17 前)抛出 std::bad_weak_ptr 异常(通过 shared_ptr 从默认构造的 weak_this 的构造函数) (C++17 起)。
enable_shared_from_this 提供安全的替用方案,以替代 std::shared_ptr
- #include <memory>
- #include <iostream>
-
- struct Good: std::enable_shared_from_this<Good> // 注意:继承
- {
- std::shared_ptr<Good> getptr()
- {
- return shared_from_this();
- }
- };
-
- struct Bad
- {
- // 错误写法:用不安全的表达式试图获得 this 的 shared_ptr 对象
- std::shared_ptr<Bad> getptr()
- {
- return std::shared_ptr<Bad>(this);
- }
- ~Bad()
- {
- std::cout << "Bad::~Bad() called\n";
- }
- };
-
- int main()
- {
- // 正确的示例:两个 shared_ptr 对象将会共享同一对象
- std::shared_ptr<Good> gp1 = std::make_shared<Good>();
- std::shared_ptr<Good> gp2 = gp1->getptr();
- std::cout << "gp2.use_count() = " << gp2.use_count() << '\n';
-
- // 错误的使用示例:调用 shared_from_this 但其没有被 std::shared_ptr 占有
- try
- {
- Good not_so_good;
- std::shared_ptr<Good> gp1 = not_so_good.getptr();
- }
- catch (std::bad_weak_ptr& e)
- {
- // C++17 前为未定义行为; C++17 起抛出 std::bad_weak_ptr 异常
- std::cout << e.what() << '\n';
- }
-
- // 错误的示例,每个 shared_ptr 都认为自己是对象仅有的所有者
- std::shared_ptr<Bad> bp1 = std::make_shared<Bad>();
- std::shared_ptr<Bad> bp2 = bp1->getptr();
- std::cout << "bp2.use_count() = " << bp2.use_count() << '\n';
- } // UB : Bad 对象将会被删除两次
