{"id":306320,"date":"2020-07-02T15:01:28","date_gmt":"2020-07-02T15:01:28","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=306320"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=306320","title":{"rendered":"\u041e\u0445 \u0443\u0436 \u044d\u0442\u043e\u0442 std::make_shared\u2026"},"content":{"rendered":"\n<div class=\"post__text post__text-html post__text_v1\" id=\"post-content-body\" data-io-article-url=\"https:\/\/habr.com\/ru\/post\/509004\/\">C++ Core Guidelines \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442 \u043f\u0440\u0430\u0432\u0438\u043b\u043e <a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Rr-make_shared\" rel=\"nofollow\">R22<\/a>, \u043f\u0440\u0435\u0434\u043f\u0438\u0441\u044b\u0432\u0430\u044e\u0449\u0435\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c std::make_shared \u0432\u043c\u0435\u0441\u0442\u043e \u0432\u044b\u0437\u043e\u0432\u0430 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\u0430 std::shared_ptr. \u0412 Core Guidelines \u043f\u0440\u0438\u0432\u043e\u0434\u0438\u0442\u0441\u044f \u0432\u0441\u0435\u0433\u043e \u043b\u0438\u0448\u044c \u043e\u0434\u0438\u043d \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442 \u0437\u0430 \u0442\u0430\u043a\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u2014 \u044d\u043a\u043e\u043d\u043e\u043c\u0438\u044f \u043d\u0430 \u0430\u043b\u043b\u043e\u043a\u0430\u0446\u0438\u0438 (\u0438 \u0434\u0435\u0430\u043b\u043b\u043e\u043a\u0430\u0446\u0438\u0438).<\/p>\n<p>  \u0410 \u0435\u0441\u043b\u0438 \u043a\u043e\u043f\u043d\u0443\u0442\u044c \u0447\u0443\u0442\u044c \u0433\u043b\u0443\u0431\u0436\u0435?<br \/>  <a name=\"habracut\"><\/a>  <\/p>\n<h2>std::make_shared \u043f\u043e\u043b\u0435\u0437\u043d\u044b\u0439<\/h2>\n<p>  <b>\u041f\u043e\u0447\u0435\u043c\u0443 \u0432\u043e\u043e\u0431\u0449\u0435 \u0432 STL \u043f\u043e\u044f\u0432\u0438\u043b\u0441\u044f std::make_shared?<\/b><\/p>\n<p>  \u0415\u0441\u0442\u044c \u043a\u0430\u043d\u043e\u043d\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u043f\u0440\u0438\u043c\u0435\u0440, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 std::shared_ptr \u0438\u0437 \u0441\u0432\u0435\u0436\u0435\u0441\u043e\u0437\u0434\u0430\u043d\u043d\u043e\u0433\u043e \u0441\u044b\u0440\u043e\u0433\u043e \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f \u043c\u043e\u0436\u0435\u0442 \u043f\u0440\u0438\u0432\u043e\u0434\u0438\u0442\u044c \u043a \u0443\u0442\u0435\u0447\u043a\u0435 \u043f\u0430\u043c\u044f\u0442\u0438:<\/p>\n<pre><code class=\"cpp\">process(std::shared_ptr&lt;Bar&gt;(new Bar), foo()); <\/code><\/pre>\n<p>  \u0414\u043b\u044f \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044f \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 process(&#8230;) \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0432\u044b\u0437\u0432\u0430\u0442\u044c:<\/p>\n<ul>\n<li>new Bar;<\/li>\n<li>\u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 std::shared_ptr;<\/li>\n<li>foo().<\/li>\n<\/ul>\n<p>  \u041a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 \u043c\u043e\u0436\u0435\u0442 \u0438\u0445 \u043f\u0435\u0440\u0435\u043c\u0435\u0448\u0438\u0432\u0430\u0442\u044c \u0432 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u043b\u044c\u043d\u043e\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u0435, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u0432\u043e\u0442 \u0442\u0430\u043a:<\/p>\n<ul>\n<li>new Bar;<\/li>\n<li>foo();<\/li>\n<li>\u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 std::shared_ptr.<\/li>\n<\/ul>\n<p>  \u0415\u0441\u043b\u0438 \u043f\u0440\u0438 \u044d\u0442\u043e\u043c \u0432 foo() \u0432\u043e\u0437\u043d\u0438\u043a\u043d\u0435\u0442 \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 \u2014 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0443\u0442\u0435\u0447\u043a\u0443 \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440\u0430 Bar.<\/p>\n<p>  \u041d\u0438 \u043e\u0434\u0438\u043d \u0438\u0437 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0445 \u043f\u0440\u0438\u043c\u0435\u0440\u043e\u0432 \u043a\u043e\u0434\u0430 \u043d\u0435 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u043f\u043e\u0442\u0435\u043d\u0446\u0438\u0430\u043b\u044c\u043d\u0443\u044e \u0443\u0442\u0435\u0447\u043a\u0443 (\u043d\u043e \u043c\u044b \u0435\u0449\u0451 \u0432\u0435\u0440\u043d\u0451\u043c\u0441\u044f \u043a \u044d\u0442\u043e\u043c\u0443 \u0432\u043e\u043f\u0440\u043e\u0441\u0443):<\/p>\n<pre><code class=\"cpp\">auto bar = std::shared_ptr&lt;Bar&gt;(new Bar); <\/code><\/pre>\n<p>  <\/p>\n<pre><code class=\"cpp\">auto bar = std::shared_ptr&lt;Bar&gt;(new Bar); process(bar, foo()); <\/code><\/pre>\n<p>  <\/p>\n<pre><code class=\"cpp\">process(std::shared_ptr&lt;Bar&gt;(new Bar)); <\/code><\/pre>\n<p>  \u041f\u043e\u0432\u0442\u043e\u0440\u044e\u0441\u044c: \u0434\u043b\u044f \u0432\u043e\u0437\u043d\u0438\u043a\u043d\u043e\u0432\u0435\u043d\u0438\u044f \u043f\u043e\u0442\u0435\u043d\u0446\u0438\u0430\u043b\u044c\u043d\u043e\u0439 \u0443\u0442\u0435\u0447\u043a\u0438 \u043d\u0430\u0434\u043e \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0438\u043c\u0435\u043d\u043d\u043e \u0442\u0430\u043a\u043e\u0439 \u043a\u043e\u0434, \u043a\u0430\u043a \u0432 \u0441\u0430\u043c\u043e\u043c \u043f\u0435\u0440\u0432\u043e\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u2014 \u043e\u0434\u043d\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u0442 \u043a\u0430\u043a \u043c\u0438\u043d\u0438\u043c\u0443\u043c \u0434\u0432\u0430 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430, \u043e\u0434\u0438\u043d \u0438\u0437 \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u0441\u0432\u0435\u0436\u0435\u0441\u043e\u0437\u0434\u0430\u043d\u043d\u044b\u043c \u0431\u0435\u0437\u044b\u043c\u044f\u043d\u043d\u044b\u043c std::shared_ptr, \u0430 \u0432\u0442\u043e\u0440\u043e\u0439 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440 \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u0432\u044b\u0437\u043e\u0432\u043e\u043c \u0434\u0440\u0443\u0433\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043c\u043e\u0436\u0435\u0442 \u0431\u0440\u043e\u0441\u0430\u0442\u044c \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f.<\/p>\n<p>  \u0410 \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u0442\u0435\u043d\u0446\u0438\u0430\u043b\u044c\u043d\u0430\u044f \u0443\u0442\u0435\u0447\u043a\u0430 \u043f\u0430\u043c\u044f\u0442\u0438 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043b\u0430\u0441\u044c \u2014 \u043d\u0430\u0434\u043e \u0435\u0449\u0451 \u0434\u0432\u0430 \u0443\u0441\u043b\u043e\u0432\u0438\u044f:<\/p>\n<ul>\n<li>\u0447\u0442\u043e\u0431\u044b \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 \u043f\u0435\u0440\u0435\u043c\u0435\u0448\u0430\u043b \u0432\u044b\u0437\u043e\u0432\u044b \u043d\u0435\u0431\u043b\u0430\u0433\u043e\u043f\u0440\u0438\u044f\u0442\u043d\u044b\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c;<\/li>\n<li>\u0447\u0442\u043e\u0431\u044b \u0444\u0443\u043d\u043a\u0446\u0438\u044f, \u0432\u044b\u0447\u0438\u0441\u043b\u044f\u044e\u0449\u0430\u044f \u0432\u0442\u043e\u0440\u043e\u0439 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440, \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0431\u0440\u043e\u0441\u0438\u043b\u0430 \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435.<\/li>\n<\/ul>\n<p>  \u0422\u0430\u043a\u043e\u0439 \u043e\u043f\u0430\u0441\u043d\u044b\u0439 \u043a\u043e\u0434 \u0432\u0440\u044f\u0434 \u043b\u0438 \u0432\u0441\u0442\u0440\u0435\u0447\u0430\u0435\u0442\u0441\u044f \u0447\u0430\u0449\u0435, \u0447\u0435\u043c \u043e\u0434\u0438\u043d \u0440\u0430\u0437 \u043d\u0430 \u0441\u0442\u043e \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u0439 std::shared_ptr.<br \/>  \u0418 \u0434\u043b\u044f \u043a\u043e\u043c\u043f\u0435\u043d\u0441\u0430\u0446\u0438\u0438 \u0432\u043e\u0442 \u044d\u0442\u043e\u0439 \u0432\u043e\u0442 \u043e\u043f\u0430\u0441\u043d\u043e\u0441\u0442\u0438 std::shared_ptr \u0431\u044b\u043b \u043f\u043e\u0434\u043f\u0451\u0440\u0442 \u043a\u043e\u0441\u0442\u044b\u043b\u0451\u043c \u043f\u043e\u0434 \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u0435\u043c std::make_shared.<\/p>\n<p>  \u0427\u0442\u043e\u0431\u044b \u0441\u043b\u0435\u0433\u043a\u0430 \u043f\u043e\u0434\u0441\u043b\u0430\u0441\u0442\u0438\u0442\u044c \u043f\u0438\u043b\u044e\u043b\u044e, \u043a \u043e\u043f\u0438\u0441\u0430\u043d\u0438\u044e std::make_shared \u0432 \u0421\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u0435 \u0434\u043e\u0431\u0430\u0432\u0438\u043b\u0438 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0443\u044e \u0444\u0440\u0430\u0437\u0443:  <\/p>\n<blockquote><p>Remarks: Implementations should perform no more than one memory allocation.<\/p><\/blockquote>\n<p>  <\/p>\n<blockquote><p>\u041f\u0440\u0438\u043c\u0435\u0447\u0430\u043d\u0438\u0435: \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f\u043c \u0441\u043b\u0435\u0434\u0443\u0435\u0442 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u044c \u043d\u0435 \u0431\u043e\u043b\u0435\u0435 \u043e\u0434\u043d\u043e\u0433\u043e \u0432\u044b\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u043f\u0430\u043c\u044f\u0442\u0438.  <\/p><\/blockquote>\n<p>  \u041d\u0435\u0442, \u044d\u0442\u043e \u043d\u0435 \u0433\u0430\u0440\u0430\u043d\u0442\u0438\u044f.<br \/>  \u041d\u043e \u043d\u0430 cppreference \u0433\u043e\u0432\u043e\u0440\u0438\u0442\u0441\u044f, \u0447\u0442\u043e \u0432\u0441\u0435 \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u044b\u0435 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0434\u0435\u043b\u0430\u044e\u0442 \u0438\u043c\u0435\u043d\u043d\u043e \u0442\u0430\u043a.<\/p>\n<p>  \u042d\u0442\u043e \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u043d\u0430\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u043e \u043d\u0430 \u043f\u043e\u0432\u044b\u0448\u0435\u043d\u0438\u0435 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u043f\u043e \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044e \u0441 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435\u043c std::shared_ptr \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0432\u044b\u0437\u043e\u0432\u0430 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\u0430, \u0442\u0440\u0435\u0431\u0443\u044e\u0449\u0438\u043c \u043c\u0438\u043d\u0438\u043c\u0443\u043c \u0434\u0432\u0435 \u0430\u043b\u043b\u043e\u043a\u0430\u0446\u0438\u0438: \u043e\u0434\u043d\u0443 \u2014 \u0434\u043b\u044f \u0440\u0430\u0437\u043c\u0435\u0449\u0435\u043d\u0438\u044f \u043e\u0431\u044a\u0435\u043a\u0442\u0430, \u0432\u0442\u043e\u0440\u0443\u044e \u2014 \u0434\u043b\u044f control block.<\/p>\n<h2>std::make_shared \u0431\u0435\u0441\u043f\u043e\u043b\u0435\u0437\u043d\u044b\u0439<\/h2>\n<p>  \u041d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 c++17 \u0443\u0442\u0435\u0447\u043a\u0430 \u043f\u0430\u043c\u044f\u0442\u0438 \u0432 \u0442\u043e\u043c \u0445\u0438\u0442\u0440\u043e\u043c \u0440\u0435\u0434\u043a\u043e\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435, \u0440\u0430\u0434\u0438 \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u0432 STL \u0431\u044b\u043b \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d std::make_shared, \u0443\u0436\u0435 \u043d\u0435\u0432\u043e\u0437\u043c\u043e\u0436\u043d\u0430.<\/p>\n<p>  \u0421\u0441\u044b\u043b\u043a\u0438 \u0434\u043b\u044f \u0438\u0437\u0443\u0447\u0435\u043d\u0438\u044f:<\/p>\n<ul>\n<li><a href=\"https:\/\/en.cppreference.com\/w\/cpp\/memory\/shared_ptr\/make_shared\" rel=\"nofollow\">\u0414\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044f \u043d\u0430 cppreference.com<\/a> \u2014 \u0438\u0441\u043a\u0430\u0442\u044c \u043f\u043e \u00abuntil C++17\u00bb;<\/li>\n<li><a href=\"https:\/\/habr.com\/ru\/company\/pvs-studio\/blog\/495570\/\">\u0413\u043b\u0443\u0431\u0438\u043d\u0430 \u043a\u0440\u043e\u043b\u0438\u0447\u044c\u0435\u0439 \u043d\u043e\u0440\u044b \u0438\u043b\u0438 \u0441\u043e\u0431\u0435\u0441\u0435\u0434\u043e\u0432\u0430\u043d\u0438\u0435 \u043f\u043e C++ \u0432 \u043a\u043e\u043c\u043f\u0430\u043d\u0438\u0438 PVS-Studio<\/a><\/li>\n<li><a href=\"https:\/\/en.cppreference.com\/w\/cpp\/language\/eval_order\" rel=\"nofollow\">\u0415\u0449\u0451 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044f \u043d\u0430 cppreference.com<\/a> \u2014 \u043f\u0443\u043d\u043a\u0442 15.<\/li>\n<\/ul>\n<p>  \u0415\u0441\u0442\u044c \u0438 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0434\u0440\u0443\u0433\u0438\u0445 \u0441\u043b\u0443\u0447\u0430\u0435\u0432, \u0432 \u043a\u043e\u0442\u043e\u0440\u044b\u0445 std::make_shared \u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0431\u0435\u0441\u043f\u043e\u043b\u0435\u0437\u0435\u043d:<\/p>\n<div class=\"spoiler\" role=\"button\" tabindex=\"0\">                         <b class=\"spoiler_title\">std::make_shared \u043d\u0435 \u0441\u043c\u043e\u0436\u0435\u0442 \u0432\u044b\u0437\u0432\u0430\u0442\u044c private \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440<\/b>                         <\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"cpp\">#include &lt;memory&gt;  class Bar { public:     static std::shared_ptr&lt;Bar&gt; create()     {         \/\/ return std::make_shared&lt;Bar&gt;(); - no build         return std::shared_ptr&lt;Bar&gt;(new Bar);     }  private:     Bar() = default; };  int main() {     auto bar = Bar::create();      return 0; } <\/code><\/pre>\n<p>  <\/div>\n<\/p><\/div>\n<p>  <\/p>\n<div class=\"spoiler\" role=\"button\" tabindex=\"0\">                         <b class=\"spoiler_title\">std::make_shared \u043d\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442 custom deleters<\/b>                         <\/p>\n<div class=\"spoiler_text\">\u2026 \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f variadic template. \u0412 \u043e\u0431\u0449\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043d\u0435\u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e \u0440\u0430\u0437\u043e\u0431\u0440\u0430\u0442\u044c\u0441\u044f, \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u044b \u043b\u0438 \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b \u0434\u043b\u044f \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0438 \u0432 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440, \u0438\u043b\u0438 \u0435\u0449\u0451 \u0438 deleter.<br \/>  \u0425\u043e\u0442\u044f \u043c\u043e\u0436\u043d\u043e \u0431\u044b\u043b\u043e \u0431\u044b \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0435\u0449\u0451 \u0438 std::make_shared_with_custom_deleter\u2026  <\/div>\n<\/p><\/div>\n<p>  \u0425\u043e\u0440\u043e\u0448\u043e \u0445\u043e\u0442\u044c \u0443\u0437\u043d\u0430\u0435\u0442\u0435 \u043e\u0431 \u044d\u0442\u0438\u0445 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0430\u0445 \u0432 compile time\u2026<\/p>\n<h2>std::make_shared \u0432\u0440\u0435\u0434\u043d\u044b\u0439<\/h2>\n<p>  \u041f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u043c \u0432 \u0440\u0430\u043d\u0442\u0430\u0439\u043c.<\/p>\n<div class=\"spoiler\" role=\"button\" tabindex=\"0\">                         <b class=\"spoiler_title\">\u043f\u0435\u0440\u0435\u0433\u0440\u0443\u0436\u0435\u043d\u043d\u044b\u0435 operator new \u0438 operator delete \u0431\u0443\u0434\u0443\u0442 \u043f\u0440\u043e\u0438\u0433\u043d\u043e\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u044b std::make_shared<\/b>                         <\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"cpp\">#include &lt;memory&gt; #include &lt;iostream&gt;  class Bar { public:     void* operator new(size_t)     {         std::cout &lt;&lt; __func__ &lt;&lt; std::endl;         return ::new Bar();     }      void operator delete(void* bar)     {         std::cout &lt;&lt; __func__ &lt;&lt; std::endl;         ::delete static_cast&lt;Bar*&gt;(bar);     } };  int main() {     auto bar = std::shared_ptr&lt;Bar&gt;(new Bar);     \/\/ auto bar = std::make_shared&lt;Bar&gt;();      return 0; } <\/code><\/pre>\n<p>  \u0412\u044b\u0432\u043e\u0434 \u0432 \u043a\u043e\u043d\u0441\u043e\u043b\u044c \u043f\u0440\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\u0430 std::shared_ptr:  <\/p>\n<blockquote><p>operator new<br \/>  operator delete  <\/p><\/blockquote>\n<p>  \u0412\u044b\u0432\u043e\u0434 \u0432 \u043a\u043e\u043d\u0441\u043e\u043b\u044c \u043f\u0440\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 std::make_shared:<br \/>  <b>\u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0443\u0435\u0442<\/b>  <\/div>\n<\/p><\/div>\n<p>  \u0410 \u0442\u0435\u043f\u0435\u0440\u044c \u2014 \u0441\u0430\u043c\u043e\u0435 \u0433\u043b\u0430\u0432\u043d\u043e\u0435, \u0440\u0430\u0434\u0438 \u0447\u0435\u0433\u043e \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u0441\u0442\u0430\u0442\u044c\u044f \u0438 \u0437\u0430\u0442\u0435\u0432\u0430\u043b\u0430\u0441\u044c.<\/p>\n<p>  \u0423\u0434\u0438\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e, \u043d\u043e \u0444\u0430\u043a\u0442: \u0442\u043e, \u043a\u0430\u043a std::shared_ptr \u0431\u0443\u0434\u0435\u0442 \u043e\u0431\u0440\u0430\u0449\u0430\u0442\u044c\u0441\u044f \u0441 \u043f\u0430\u043c\u044f\u0442\u044c\u044e, \u043c\u043e\u0436\u0435\u0442 <b>\u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u043e<\/b> \u0437\u0430\u0432\u0438\u0441\u0435\u0442\u044c \u043e\u0442 \u0442\u043e\u0433\u043e, <b>\u043a\u0430\u043a \u0438\u043c\u0435\u043d\u043d\u043e<\/b> \u043e\u043d \u0431\u044b\u043b \u0441\u043e\u0437\u0434\u0430\u043d \u2014 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e std::make_shared \u0438\u043b\u0438 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\u0430!<\/p>\n<p>  <b>\u041f\u043e\u0447\u0435\u043c\u0443 \u0442\u0430\u043a \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442?<\/b><\/p>\n<p>  \u041f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u00ab\u043f\u043e\u043b\u0435\u0437\u043d\u0430\u044f\u00bb \u0435\u0434\u0438\u043d\u0430\u044f \u0430\u043b\u043b\u043e\u043a\u0430\u0446\u0438\u044f, \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u043c\u0430\u044f std::make_shared, \u0438\u043c\u0435\u0435\u0442 \u043d\u0435\u043e\u0442\u044a\u0435\u043c\u043b\u0435\u043c\u044b\u0439 \u043f\u043e\u0431\u043e\u0447\u043d\u044b\u0439 \u044d\u0444\u0444\u0435\u043a\u0442 \u0432 \u0432\u0438\u0434\u0435 \u0432\u043e\u0437\u043d\u0438\u043a\u043d\u043e\u0432\u0435\u043d\u0438\u044f \u043b\u0438\u0448\u043d\u0435\u0439 \u0441\u0432\u044f\u0437\u0438 \u043c\u0435\u0436\u0434\u0443 control block \u0438 \u0443\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u043c\u044b\u043c \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u043c. \u041e\u043d\u0438 \u043f\u0440\u043e\u0441\u0442\u043e \u043d\u0435 \u043c\u043e\u0433\u0443\u0442 \u0431\u044b\u0442\u044c \u043e\u0441\u0432\u043e\u0431\u043e\u0436\u0434\u0435\u043d\u044b \u043f\u043e \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438. \u0410 control block \u043e\u0431\u044f\u0437\u0430\u043d \u0436\u0438\u0442\u044c \u0434\u043e \u0442\u0435\u0445 \u043f\u043e\u0440, \u043f\u043e\u043a\u0430 \u0435\u0441\u0442\u044c \u0445\u043e\u0442\u044f \u0431\u044b \u043e\u0434\u043d\u0430 \u0441\u043b\u0430\u0431\u0430\u044f \u0441\u0441\u044b\u043b\u043a\u0430.<\/p>\n<p>  \u041e\u0442 std::shared_ptr, \u0441\u043e\u0437\u0434\u0430\u043d\u043d\u043e\u0433\u043e \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\u0430, \u0441\u043b\u0435\u0434\u0443\u0435\u0442 \u043e\u0436\u0438\u0434\u0430\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0433\u043e \u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u044f:<\/p>\n<ul>\n<li>\u0430\u043b\u043b\u043e\u043a\u0430\u0446\u0438\u044f \u0443\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u043c\u043e\u0433\u043e \u043e\u0431\u044a\u0435\u043a\u0442\u0430 (\u0434\u043e \u0432\u044b\u0437\u043e\u0432\u0430 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\u0430, \u0442.\u0435. \u043d\u0430 \u0441\u0442\u043e\u0440\u043e\u043d\u0435 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f);<\/li>\n<li>\u0430\u043b\u043b\u043e\u043a\u0430\u0446\u0438\u044f \u0431\u043b\u043e\u043a\u0430 \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f;<\/li>\n<li>\u043f\u0440\u0438 \u0443\u043d\u0438\u0447\u0442\u043e\u0436\u0435\u043d\u0438\u0438 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0439 \u0441\u0438\u043b\u044c\u043d\u043e\u0439 \u0441\u0441\u044b\u043b\u043a\u0438 \u2014 \u0432\u044b\u0437\u043e\u0432 \u0434\u0435\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\u0430 \u0443\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u043c\u043e\u0433\u043e \u043e\u0431\u044a\u0435\u043a\u0442\u0430 <b>\u0438 \u043e\u0441\u0432\u043e\u0431\u043e\u0436\u0434\u0435\u043d\u0438\u0435 \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u043c\u043e\u0439 \u0438\u043c \u043f\u0430\u043c\u044f\u0442\u0438<\/b>; \u0435\u0441\u043b\u0438 \u043f\u0440\u0438 \u044d\u0442\u043e\u043c \u043d\u0435\u0442 \u043d\u0438 \u043e\u0434\u043d\u043e\u0439 \u0441\u043b\u0430\u0431\u043e\u0439 \u0441\u0441\u044b\u043b\u043a\u0438 \u2014 \u043e\u0441\u0432\u043e\u0431\u043e\u0436\u0434\u0435\u043d\u0438\u0435 \u0431\u043b\u043e\u043a\u0430 \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f;<\/li>\n<li>\u043f\u0440\u0438 \u0443\u043d\u0438\u0447\u0442\u043e\u0436\u0435\u043d\u0438\u0438 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0439 \u0441\u043b\u0430\u0431\u043e\u0439 \u0441\u0441\u044b\u043b\u043a\u0438 \u043f\u0440\u0438 \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0438\u0438 \u0441\u0438\u043b\u044c\u043d\u044b\u0445 \u0441\u0441\u044b\u043b\u043e\u043a \u2014 \u043e\u0441\u0432\u043e\u0431\u043e\u0436\u0434\u0435\u043d\u0438\u0435 \u0431\u043b\u043e\u043a\u0430 \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f.<\/li>\n<\/ul>\n<p>  \u0410 \u0432 \u0441\u043b\u0443\u0447\u0430\u0435 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e std::make_shared:<\/p>\n<ul>\n<li>\u0430\u043b\u043b\u043e\u043a\u0430\u0446\u0438\u044f \u0443\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u043c\u043e\u0433\u043e \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u0438 \u0431\u043b\u043e\u043a\u0430 \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f;<\/li>\n<li>\u043f\u0440\u0438 \u0443\u043d\u0438\u0447\u0442\u043e\u0436\u0435\u043d\u0438\u0438 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0439 \u0441\u0438\u043b\u044c\u043d\u043e\u0439 \u0441\u0441\u044b\u043b\u043a\u0438 \u2014 \u0432\u044b\u0437\u043e\u0432 \u0434\u0435\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\u0430 \u0443\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u043c\u043e\u0433\u043e \u043e\u0431\u044a\u0435\u043a\u0442\u0430 <b>\u0431\u0435\u0437 \u043e\u0441\u0432\u043e\u0431\u043e\u0436\u0434\u0435\u043d\u0438\u044f \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u043c\u043e\u0439 \u0438\u043c \u043f\u0430\u043c\u044f\u0442\u0438<\/b>; \u0435\u0441\u043b\u0438 \u043f\u0440\u0438 \u044d\u0442\u043e\u043c \u043d\u0435\u0442 \u043d\u0438 \u043e\u0434\u043d\u043e\u0439 \u0441\u043b\u0430\u0431\u043e\u0439 \u0441\u0441\u044b\u043b\u043a\u0438 \u2014 \u043e\u0441\u0432\u043e\u0431\u043e\u0436\u0434\u0435\u043d\u0438\u0435 \u0431\u043b\u043e\u043a\u0430 \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0438 \u043f\u0430\u043c\u044f\u0442\u0438 \u0443\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u043c\u043e\u0433\u043e \u043e\u0431\u044a\u0435\u043a\u0442\u0430;<\/li>\n<li>\u043f\u0440\u0438 \u0443\u043d\u0438\u0447\u0442\u043e\u0436\u0435\u043d\u0438\u0438 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0439 \u0441\u043b\u0430\u0431\u043e\u0439 \u0441\u0441\u044b\u043b\u043a\u0438 \u043f\u0440\u0438 \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0438\u0438 \u0441\u0438\u043b\u044c\u043d\u044b\u0445 \u0441\u0441\u044b\u043b\u043e\u043a \u2014 \u043e\u0441\u0432\u043e\u0431\u043e\u0436\u0434\u0435\u043d\u0438\u0435 \u0431\u043b\u043e\u043a\u0430 \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0438 \u043f\u0430\u043c\u044f\u0442\u0438 \u0443\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u043c\u043e\u0433\u043e \u043e\u0431\u044a\u0435\u043a\u0442\u0430.<\/li>\n<\/ul>\n<p>  \u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 std::shared_ptr \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e std::make_shared \u043f\u0440\u043e\u0432\u043e\u0446\u0438\u0440\u0443\u0435\u0442 space leak.<\/p>\n<p>  \u0420\u0430\u0437\u043b\u0438\u0447\u0438\u0442\u044c \u0432 \u0440\u0430\u043d\u0442\u0430\u0439\u043c\u0435, \u043a\u0430\u043a \u0438\u043c\u0435\u043d\u043d\u043e \u0431\u044b\u043b \u0441\u043e\u0437\u0434\u0430\u043d \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440 std::shared_ptr, \u043d\u0435\u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e.<\/p>\n<p>  \u041f\u0435\u0440\u0435\u0439\u0434\u0451\u043c \u043a \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0435 \u044d\u0442\u043e\u0433\u043e \u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u044f.<\/p>\n<p>  \u0415\u0441\u0442\u044c \u043e\u0447\u0435\u043d\u044c \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u0441\u043f\u043e\u0441\u043e\u0431 \u2014 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c std::allocate_shared \u0441 custom allocator, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u043e\u0431\u0449\u0430\u0442\u044c \u043e\u0431\u043e \u0432\u0441\u0435\u0445 \u043e\u0431\u0440\u0430\u0449\u0435\u043d\u0438\u044f\u0445 \u043a \u043d\u0435\u043c\u0443. \u041d\u043e \u0432\u043e\u0442 \u0440\u0430\u0441\u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u044f\u0442\u044c \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u044b\u0435 \u0442\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b \u043d\u0430 std::make_shared \u043d\u0435\u043a\u043e\u0440\u0440\u0435\u043a\u0442\u043d\u043e.<\/p>\n<p>  \u0411\u043e\u043b\u0435\u0435 \u043a\u043e\u0440\u0440\u0435\u043a\u0442\u043d\u044b\u0439 \u0441\u043f\u043e\u0441\u043e\u0431 \u2014 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u044c \u0441\u0443\u043c\u043c\u0430\u0440\u043d\u043e\u0433\u043e \u0440\u0430\u0441\u0445\u043e\u0434\u0430 \u043f\u0430\u043c\u044f\u0442\u0438. \u041d\u043e \u043d\u0438 \u043e \u043a\u0430\u043a\u043e\u0439 \u043a\u0440\u043e\u0441\u0441-\u043f\u043b\u0430\u0442\u0444\u043e\u0440\u043c\u0435\u043d\u043d\u043e\u0441\u0442\u0438 \u0442\u0443\u0442 \u043d\u0435 \u0438\u0434\u0451\u0442 \u0440\u0435\u0447\u0438.<\/p>\n<p>  \u041f\u0440\u0438\u0432\u0435\u0434\u0451\u043d \u043a\u043e\u0434 \u0434\u043b\u044f Linux, \u043f\u0440\u043e\u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u043d\u0430 Ubuntu 20.04 desktop x64. \u041a\u043e\u043c\u0443 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e \u043f\u043e\u0432\u0442\u043e\u0440\u0438\u0442\u044c \u044d\u0442\u043e \u0434\u043b\u044f \u0434\u0440\u0443\u0433\u0438\u0445 \u043f\u043b\u0430\u0442\u0444\u043e\u0440\u043c \u2014 \u0441\u043c\u043e\u0442\u0440\u0438\u0442\u0435 <a href=\"https:\/\/stackoverflow.com\/questions\/63166\/how-to-determine-cpu-and-memory-consumption-from-inside-a-process\" rel=\"nofollow\">\u0442\u0443\u0442<\/a> (\u043c\u043e\u0438 \u044d\u043a\u0441\u043f\u0435\u0440\u0438\u043c\u0435\u043d\u0442\u044b \u0441 macOs \u043f\u043e\u043a\u0430\u0437\u0430\u043b\u0438, \u0447\u0442\u043e \u043e\u043f\u0446\u0438\u044f TASK_BASIC_INFO \u043d\u0435 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043e\u0442\u0441\u043b\u0435\u0436\u0438\u0432\u0430\u0442\u044c \u043e\u0441\u0432\u043e\u0431\u043e\u0436\u0434\u0435\u043d\u0438\u0435 \u043f\u0430\u043c\u044f\u0442\u0438, \u0438 TASK_VM_INFO_PURGEABLE \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0431\u043e\u043b\u0435\u0435 \u043f\u043e\u0434\u0445\u043e\u0434\u044f\u0449\u0438\u043c \u043a\u0430\u043d\u0434\u0438\u0434\u0430\u0442\u043e\u043c).<\/p>\n<div class=\"spoiler\" role=\"button\" tabindex=\"0\">                         <b class=\"spoiler_title\">Monitoring.h<\/b>                         <\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"cpp\">#pragma once  #include &lt;cstdint&gt;  uint64_t memUsage(); <\/code><\/pre>\n<p>  <\/div>\n<\/p><\/div>\n<p>  <\/p>\n<div class=\"spoiler\" role=\"button\" tabindex=\"0\">                         <b class=\"spoiler_title\">Monitoring.cpp<\/b>                         <\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"cpp\">#include &quot;Monitoring.h&quot;  #include &lt;fstream&gt; #include &lt;string&gt;  uint64_t memUsage() {     auto file = std::ifstream(&quot;\/proc\/self\/status&quot;, std::ios_base::in);     auto line = std::string();      while(std::getline(file, line)) {         if (line.find(&quot;VmSize&quot;) != std::string::npos) {             std::string toConvert;             for (const auto&amp; elem : line) {                 if (std::isdigit(elem)) {                     toConvert += elem;                 }             }             return stoull(toConvert);         }     }      return 0; } <\/code><\/pre>\n<p>  <\/div>\n<\/p><\/div>\n<p>  <\/p>\n<div class=\"spoiler\" role=\"button\" tabindex=\"0\">                         <b class=\"spoiler_title\">main.cpp<\/b>                         <\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"cpp\">#include &lt;iostream&gt; #include &lt;array&gt; #include &lt;numeric&gt; #include &lt;memory&gt;  #include &quot;Monitoring.h&quot;  struct Big {     ~Big()     {         std::cout &lt;&lt; __func__ &lt;&lt; std::endl;     }      std::array&lt;volatile unsigned char, 64*1024*1024&gt; _data; };  volatile uint64_t accumulator = 0;  int main() {     std::cout &lt;&lt; &quot;initial: &quot; &lt;&lt; memUsage() &lt;&lt; std::endl;      auto strong = std::shared_ptr&lt;Big&gt;(new Big);     \/\/ auto strong = std::make_shared&lt;Big&gt;();      std::accumulate(strong-&gt;_data.cbegin(), strong-&gt;_data.cend(), accumulator);      auto weak = std::weak_ptr&lt;Big&gt;(strong);      std::cout &lt;&lt; &quot;before reset: &quot; &lt;&lt; memUsage() &lt;&lt; std::endl;      strong.reset();      std::cout &lt;&lt; &quot;after strong reset: &quot; &lt;&lt; memUsage() &lt;&lt; std::endl;      weak.reset();      std::cout &lt;&lt; &quot;after weak reset: &quot; &lt;&lt; memUsage() &lt;&lt; std::endl;      return 0; } <\/code><\/pre>\n<p>  <\/div>\n<\/p><\/div>\n<p>  \u0412\u044b\u0432\u043e\u0434 \u0432 \u043a\u043e\u043d\u0441\u043e\u043b\u044c \u043f\u0440\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\u0430 std::shared_ptr:  <\/p>\n<blockquote><p>initial: 5884<br \/>  before reset: 71424<br \/>  ~Big<br \/>  after strong reset: 5884<br \/>  after weak reset: 5884  <\/p><\/blockquote>\n<p>  \u0412\u044b\u0432\u043e\u0434 \u0432 \u043a\u043e\u043d\u0441\u043e\u043b\u044c \u043f\u0440\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 std::make_shared:  <\/p>\n<blockquote><p>initial: 5888<br \/>  before reset: 71428<br \/>  ~Big<br \/>  after strong reset: 71428<br \/>  after weak reset: 5888  <\/p><\/blockquote>\n<p>  <\/p>\n<h2>\u0411\u043e\u043d\u0443\u0441<\/h2>\n<p>  \u0410 \u0432\u0441\u0451 \u0436\u0435, \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u0430 \u043b\u0438 \u0443\u0442\u0435\u0447\u043a\u0430 \u043f\u0430\u043c\u044f\u0442\u0438 \u0432 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u043a\u043e\u0434\u0430 <\/p>\n<pre><code class=\"cpp\">auto bar = std::shared_ptr&lt;Bar&gt;(new Bar);<\/code><\/pre>\n<p>  ?<br \/>  \u0427\u0442\u043e \u043f\u0440\u043e\u0438\u0437\u043e\u0439\u0434\u0451\u0442, \u0435\u0441\u043b\u0438 \u0430\u043b\u043b\u043e\u043a\u0430\u0446\u0438\u044f Bar \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u0441\u044f \u0443\u0441\u043f\u0435\u0448\u043d\u043e, \u0430 \u0432\u043e\u0442 \u043d\u0430 control block \u043f\u0430\u043c\u044f\u0442\u0438 \u0443\u0436\u0435 \u043d\u0435 \u0445\u0432\u0430\u0442\u0438\u0442?<\/p>\n<p>  \u0410 \u0447\u0442\u043e \u043f\u0440\u043e\u0438\u0437\u043e\u0439\u0434\u0451\u0442, \u0435\u0441\u043b\u0438 \u0431\u044b\u043b \u0432\u044b\u0437\u0432\u0430\u043d \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 \u0441 custom deleter?<\/p>\n<p>  \u0420\u0430\u0437\u0434\u0435\u043b [util.smartptr.shared.const] \u0421\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u0430 \u0433\u0430\u0440\u0430\u043d\u0442\u0438\u0440\u0443\u0435\u0442, \u0447\u0442\u043e \u043f\u0440\u0438 \u0432\u043e\u0437\u043d\u0438\u043a\u043d\u043e\u0432\u0435\u043d\u0438\u0438 \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f \u0432\u043d\u0443\u0442\u0440\u0438 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\u0430 std::shared_ptr:<\/p>\n<ul>\n<li>\u0434\u043b\u044f \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\u0430 \u0431\u0435\u0437 custom deleter \u043f\u0435\u0440\u0435\u0434\u0430\u043d\u043d\u044b\u0439 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u0431\u0443\u0434\u0435\u0442 \u0443\u0434\u0430\u043b\u0451\u043d \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e delete \u0438\u043b\u0438 delete[];<\/li>\n<li>\u0434\u043b\u044f \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\u0430 \u0441 custom deleter \u043f\u0435\u0440\u0435\u0434\u0430\u043d\u043d\u044b\u0439 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u0431\u0443\u0434\u0435\u0442 \u0443\u0434\u0430\u043b\u0451\u043d \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u044d\u0442\u043e\u0433\u043e \u0441\u0430\u043c\u043e\u0433\u043e deleter.<\/li>\n<\/ul>\n<p>  \u041e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0438\u0435 \u0443\u0442\u0435\u0447\u043a\u0438 \u0433\u0430\u0440\u0430\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043e \u0421\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043e\u043c.<\/p>\n<p>  \u0412 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435 \u0431\u0435\u0433\u043b\u043e\u0433\u043e \u0447\u0442\u0435\u043d\u0438\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0439 \u0432 \u0442\u0440\u0451\u0445 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440\u0430\u0445 (Apple clang version 11.0.3, GCC 9.3.0, MSVC 2019 16.6.2) \u044f \u043c\u043e\u0433\u0443 \u043f\u043e\u0434\u0442\u0432\u0435\u0440\u0434\u0438\u0442\u044c, \u0447\u0442\u043e \u0432\u0441\u0451 \u0442\u0430\u043a \u0438 \u0435\u0441\u0442\u044c.<\/p>\n<h2>\u0412\u044b\u0432\u043e\u0434<\/h2>\n<p>  \u0412 \u0441++11 \u0438 \u0441++14 \u0432\u0440\u0435\u0434 \u043e\u0442 \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u044f std::make_shared \u043c\u043e\u0433 \u0431\u044b\u0442\u044c \u0441\u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u0430\u043d \u0435\u0434\u0438\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0439 \u0435\u0433\u043e \u043f\u043e\u043b\u0435\u0437\u043d\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0435\u0439.<\/p>\n<p>  \u041d\u0430\u0447\u0438\u043d\u0430\u044f \u0436\u0435 \u0441 c++17, \u0430\u0440\u0438\u0444\u043c\u0435\u0442\u0438\u043a\u0430 \u0441\u043e\u0432\u0441\u0435\u043c \u043d\u0435 \u0432 \u043f\u043e\u043b\u044c\u0437\u0443 std::make_shared.<\/p>\n<p>  \u0410\u043d\u0430\u043b\u043e\u0433\u0438\u0447\u043d\u0430\u044f \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u044f \u0438 \u0441 std::allocate_shared.<\/p>\n<p>  \u041c\u043d\u043e\u0433\u043e\u0435 \u0438\u0437 \u0441\u043a\u0430\u0437\u0430\u043d\u043d\u043e\u0433\u043e \u0441\u043f\u0440\u0430\u0432\u0435\u0434\u043b\u0438\u0432\u043e \u0442\u0430\u043a\u0436\u0435 \u0438 \u0434\u043b\u044f std::make_unique, \u043d\u043e \u043e\u0442 \u043d\u0435\u0433\u043e \u043c\u0435\u043d\u044c\u0448\u0435 \u0432\u0440\u0435\u0434\u0430.<\/p><\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/509004\/\"> https:\/\/habr.com\/ru\/post\/509004\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"\n<div class=\"post__text post__text-html post__text_v1\" id=\"post-content-body\" data-io-article-url=\"https:\/\/habr.com\/ru\/post\/509004\/\">C++ Core Guidelines \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442 \u043f\u0440\u0430\u0432\u0438\u043b\u043e <a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Rr-make_shared\" rel=\"nofollow\">R22<\/a>, \u043f\u0440\u0435\u0434\u043f\u0438\u0441\u044b\u0432\u0430\u044e\u0449\u0435\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c std::make_shared \u0432\u043c\u0435\u0441\u0442\u043e \u0432\u044b\u0437\u043e\u0432\u0430 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\u0430 std::shared_ptr. \u0412 Core Guidelines \u043f\u0440\u0438\u0432\u043e\u0434\u0438\u0442\u0441\u044f \u0432\u0441\u0435\u0433\u043e \u043b\u0438\u0448\u044c \u043e\u0434\u0438\u043d \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442 \u0437\u0430 \u0442\u0430\u043a\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u2014 \u044d\u043a\u043e\u043d\u043e\u043c\u0438\u044f \u043d\u0430 \u0430\u043b\u043b\u043e\u043a\u0430\u0446\u0438\u0438 (\u0438 \u0434\u0435\u0430\u043b\u043b\u043e\u043a\u0430\u0446\u0438\u0438).<\/p>\n<p>  \u0410 \u0435\u0441\u043b\u0438 \u043a\u043e\u043f\u043d\u0443\u0442\u044c \u0447\u0443\u0442\u044c \u0433\u043b\u0443\u0431\u0436\u0435?  <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-306320","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/306320","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=306320"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/306320\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=306320"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=306320"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=306320"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}