{"id":69,"date":"2021-07-15T04:31:24","date_gmt":"2021-07-14T20:31:24","guid":{"rendered":"http:\/\/139.224.63.49\/?p=69"},"modified":"2021-07-15T04:31:24","modified_gmt":"2021-07-14T20:31:24","slug":"%e8%b0%83%e8%af%95bad_weak_ptr%e6%8a%a5%e9%94%99","status":"publish","type":"post","link":"http:\/\/iamnear.top\/?p=69","title":{"rendered":"\u8c03\u8bd5bad_weak_ptr\u62a5\u9519"},"content":{"rendered":"\n<p>\u5728\u7f51\u4e0a\u7b80\u5355\u767e\u5ea6\u4e86\u4e0b\u8fd9\u4e2a\u9519\u8bef\uff0c\u53d1\u73b0\u5927\u591a\u6570\u8bf4\u7684\u662f\u4e0b\u9762\u4e24\u79cd\u60c5\u51b5<\/p>\n\n\n\n<p>\u60c5\u51b51 \u6ca1\u6709\u7528shared\u6765\u7ba1\u7406\u7684\u7c7b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class A:public enable_shared_from_this\n{};\nint main()\n{\n\tA a;\n\tauto p = a.shared_from_this();\n}<\/code><\/pre>\n\n\n\n<p>\u60c5\u51b52 \u5728\u6784\u9020\u51fd\u6570\u8c03\u7528shared_from_this<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class A:public enable_shared_from_this\n{\n\tA()\n\t{\n\t\tauto p = shared_from_this();\n\t}\n}<\/code><\/pre>\n\n\n\n<p>\u7136\u800c\u6211\u7684\u95ee\u9898\u4e0d\u662f\u8fd9\u4e24\u79cd\u60c5\u51b5\u3002\u6700\u540e\u8c03\u8bd5\u4e86\u597d\u4e45\u7ec8\u4e8e\u5f04\u660e\u767d\u95ee\u9898\u5728\u54ea\u4e86\uff0c\u8fd9\u4e2a\u8fc7\u7a0b\u4e2d\u4e5f\u5bf9shared_ptr\u548cenable_shared_from_this\u7684\u6e90\u7801\u770b\u61c2\u4e86\u4e03\u4e03\u516b\u516b\uff0c\u5bf9enable_shared_from_this\u4e5f\u7b97\u662f\u7406\u89e3\u4e86\u3002<\/p>\n\n\n\n<p>\u6240\u8c13\u7684shared_ptr\uff0c\u5176\u5b9e\u5c31\u662f\u5728\u6307\u9488\u548c\u5bf9\u8c61\u4e4b\u95f4\u52a0\u4e86\u4e00\u5c42\u3002\u6307\u9488\u8981\u5b9e\u73b0\u8ba1\u6570\uff0c\u603b\u5f97\u6709\u4e2a\u5730\u65b9\u4fdd\u5b58\u6570\u5b57\u5427\u3002\u8fd9\u4e2a\u5730\u65b9\u5f97\u8ba9\u6240\u6709\u6307\u5411\u540c\u4e00\u4e2a\u5bf9\u8c61\u7684\u667a\u80fd\u6307\u9488\u90fd\u80fd\u8bbf\u95ee\u5230\uff0c\u90a3\u53ea\u80fd\u662f\u5806\u4e86\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u53ea\u80fd\u6307\u9488\u5185\u90e8\u6709\u4e2a\u6307\u9488\uff0c\u6307\u5411\u5806\u4e2d\uff0c\u90a3\u4e2a\u5730\u65b9\u5c31\u662f\u653e\u5f15\u7528\u8ba1\u6570\u7684\u5730\u65b9\u3002\u8fd9\u4e2a\u5730\u65b9\u7684\u5730\u5740\u5c31\u662f\u5173\u952e\u3002enable_shared_form_this\u8981\u89e3\u51b3\u7684\uff0c\u5c31\u662f\u7c7b\u81ea\u5df1\u600e\u4e48\u77e5\u9053\u90a3\u4e2a\u653e\u5f15\u7528\u8ba1\u6570\u7684\u5730\u65b9\u5728\u54ea\uff1f\u901a\u8fc7\u7ee7\u627fenable_shared_form_this\uff0c\u5176\u5b9e\u7ee7\u627f\u4e86\u4e00\u4e2a\u540d\u5b57\u4e3a_M_weak_this\u7684weak_ptr\u6307\u9488\u3002\u8fd9\u4e2a\u6307\u9488\u5c31\u662f\u8981\u6307\u5411\u5bf9\u8c61\u81ea\u5df1\u7684\u6307\u9488\uff0c\u5f53\u9700\u8981shared_from_this\u7684\u65f6\u5019\uff0c\u8fd4\u56de\u7684\u5c31\u662f\u7528\u8fd9\u4e2aweakptr\u521d\u59cb\u5316\u6784\u9020\u6210\u7684sharedptr\u3002<\/p>\n\n\n\n<p>\u6211\u6700\u5f00\u59cb\u770b\u6e90\u4ee3\u7801\u5f88\u5947\u602a\u6ca1\u627e\u5230\u54ea\u91cc\u6709\u521d\u59cb\u5316\u8fd9\u4e2aweakptr\u7684\u5730\u65b9\u554a\u3002enable_shared_from_this\u7684\u6e90\u4ee3\u7801\u5f88\u5c11\uff0c\u5982\u4e0b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code> template&lt;typename _Tp>\n    class enable_shared_from_this\n    {\n    protected:\n      constexpr enable_shared_from_this() noexcept { }\n\n      enable_shared_from_this(const enable_shared_from_this&amp;) noexcept { }\n\n      enable_shared_from_this&amp;\n      operator=(const enable_shared_from_this&amp;) noexcept\n      { return *this; }\n\n      ~enable_shared_from_this() { }\n\n    public:\n      shared_ptr&lt;_Tp>\n      shared_from_this()\n      { return shared_ptr&lt;_Tp>(this->_M_weak_this); }\n\n      shared_ptr&lt;const _Tp>\n      shared_from_this() const\n      { return shared_ptr&lt;const _Tp>(this->_M_weak_this); }\n\n#if __cplusplus > 201402L || !defined(__STRICT_ANSI__) \/\/ c++1z or gnu++11\n#define __cpp_lib_enable_shared_from_this 201603\n      weak_ptr&lt;_Tp>\n      weak_from_this() noexcept\n      { return this->_M_weak_this; }\n\n      weak_ptr&lt;const _Tp>\n      weak_from_this() const noexcept\n      { return this->_M_weak_this; }\n#endif\n\n    private:\n      template&lt;typename _Tp1>\n\tvoid\n\t_M_weak_assign(_Tp1* __p, const __shared_count&lt;>&amp; __n) const noexcept\n\t{ _M_weak_this._M_assign(__p, __n); }\n\n      \/\/ Found by ADL when this is an associated class.\n      friend const enable_shared_from_this*\n      __enable_shared_from_this_base(const __shared_count&lt;>&amp;,\n\t\t\t\t     const enable_shared_from_this* __p)\n      { return __p; }\n\n      template&lt;typename, _Lock_policy>\n\tfriend class __shared_ptr;\n\n      mutable weak_ptr&lt;_Tp>  _M_weak_this;\n    };<\/code><\/pre>\n\n\n\n<p>\u540e\u6765\u770b\u4e86\u4e00\u4e9b\u6587\u7ae0\u624d\u660e\u767d\uff0c\u8fd9\u4e2a\u521d\u59cb\u5316\u662f\u5728shareptr\u7684\u6784\u9020\u51fd\u6570\u4e2d\u5b8c\u6210\u7684\u3002\u5728sharedptr\u7684\u6784\u9020\u51fd\u6570\u4e2d\uff0c\u4f1a\u68c0\u6d4b\u8981\u6784\u9020\u7684\u5bf9\u8c61\u662f\u5426\u7ee7\u627f\u4e86enable_shared_from_this.\u5982\u679c\u7ee7\u627f\u4e86\u8fd9\u4e2a\u7c7b\uff0c\u5c31\u4f1a\u5bf9\u8fd9\u4e2a\u7c7b\u4e2d\u7684_M_weak_this\u8d4b\u503c\uff0c\u8ba9\u8fd9\u4e2aweakptr\u4e2d\u8bb0\u5f55\u4e0b\u6765sharedptr\u4fdd\u5b58\u5f15\u7528\u8ba1\u6570\u7684\u5730\u65b9\u7684\u5185\u5b58\u5730\u5740\u3002\u4ece\u800c\u5b9e\u73b0\u4e86\u529f\u80fd\u3002<\/p>\n\n\n\n<p>\u8fd9\u91cc\u662f\u600e\u4e48\u5224\u65ad\u5bf9\u8c61\u6709\u6ca1\u6709\u7ee7\u627fenable_shared_from_this\u5462\uff1f<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>      template&lt;typename _Yp, typename _Yp2 = typename remove_cv&lt;_Yp>::type>\n\ttypename enable_if&lt;__has_esft_base&lt;_Yp2>::value>::type\n\t_M_enable_shared_from_this_with(_Yp* __p) noexcept\n\t{\n\t  if (auto __base = __enable_shared_from_this_base(_M_refcount, __p))\n\t    __base->_M_weak_assign(const_cast&lt;_Yp2*>(__p), _M_refcount);\n\t}\n\n      template&lt;typename _Yp, typename _Yp2 = typename remove_cv&lt;_Yp>::type>\n\ttypename enable_if&lt;!__has_esft_base&lt;_Yp2>::value>::type\n\t_M_enable_shared_from_this_with(_Yp*) noexcept\n\t{ }<\/code><\/pre>\n\n\n\n<p>\u5982\u4e0a\u7684\u4e24\u4e2a\u51fd\u6570\uff0c\u5206\u522b\u662f\u7ed9\u7ee7\u627f\u4e86enable_shared_from_this\u548c\u6ca1\u7ee7\u627fenable_shared_from_this\u7528\u7684\u3002\u53ef\u4ee5\u770b\u5230\u662f\u7528\u6a21\u677f\u7684\u65b9\u6cd5\uff0c\u5728\u7f16\u8bd1\u671f\u5b9e\u73b0\u7684\u3002\u4e0d\u8fc7\u5177\u4f53\u8fd9\u4e2a__has_esft_base\u662f\u600e\u4e48\u505a\u5230\u7684\uff0c\u6211\u6ca1\u592a\u770b\u61c2\u3002\u6a21\u677f\u771f\u7684\u662f\u9ed1\u9b54\u6cd5\u3002<\/p>\n\n\n\n<p>\u6211\u9047\u5230\u7684\u95ee\u9898\u5c31\u662f\u5728\u8fd9\u4e00\u6b65\uff0c\u5e94\u8be5\u6267\u884c\u4e0a\u4e00\u4e2a\u51fd\u6570\uff0c\u7ed3\u679c\u6267\u884c\u7684\u5374\u662f\u4e0b\u9762\u7684\u51fd\u6570\u3002\u8fd9\u8bf4\u660e\u6211\u6ca1\u6709\u6b63\u786e\u7ee7\u627fenable_shared_from_this\u554a\uff01\u6211\u7ffb\u56de\u53bb\u4e00\u770b\uff0c\u539f\u6765\u662f\u56e0\u4e3a\u6211\u6ca1\u6709\u516c\u6709\u7ee7\u627f\u3002c++\u9ed8\u8ba4\u4e0d\u5199\u7684\u8bdd\u5c31\u662f\u79c1\u6709\u7ee7\u627f\u3002\u7ec8\u4e8e\u627e\u5230\u95ee\u9898\u6240\u5728\u4e86\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u7f51\u4e0a\u7b80\u5355\u767e\u5ea6\u4e86\u4e0b\u8fd9\u4e2a\u9519\u8bef\uff0c\u53d1\u73b0\u5927\u591a\u6570\u8bf4\u7684\u662f\u4e0b\u9762\u4e24\u79cd\u60c5\u51b5 \u60c5\u51b51 \u6ca1\u6709\u7528shared\u6765\u7ba1\u7406\u7684\u7c7b \u60c5\u51b5&hellip; <a href=\"http:\/\/iamnear.top\/?p=69\" class=\"more-link read-more\" rel=\"bookmark\">\u7ee7\u7eed\u9605\u8bfb <span class=\"screen-reader-text\">\u8c03\u8bd5bad_weak_ptr\u62a5\u9519<\/span><i class=\"fa fa-arrow-right\"><\/i><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":{"0":"post-69","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"hentry","6":"category-uncategorized","7":"h-entry","9":"h-as-article"},"_links":{"self":[{"href":"http:\/\/iamnear.top\/index.php?rest_route=\/wp\/v2\/posts\/69","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/iamnear.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/iamnear.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/iamnear.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/iamnear.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=69"}],"version-history":[{"count":1,"href":"http:\/\/iamnear.top\/index.php?rest_route=\/wp\/v2\/posts\/69\/revisions"}],"predecessor-version":[{"id":70,"href":"http:\/\/iamnear.top\/index.php?rest_route=\/wp\/v2\/posts\/69\/revisions\/70"}],"wp:attachment":[{"href":"http:\/\/iamnear.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=69"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/iamnear.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=69"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/iamnear.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=69"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}