{"id":366156,"date":"2024-05-21T02:53:27","date_gmt":"2024-05-21T02:53:27","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=366156"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=366156","title":{"rendered":"<span>\u041c\u043d\u043e\u0433\u043e\u043e\u0431\u0440\u0430\u0437\u0438\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u044b\u0445 \u043e\u0431\u0451\u0440\u0442\u043e\u043a<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<h2>A polymorphic function object wrapper<\/h2>\n<p>\u0412 \u0434\u0430\u043b\u0451\u043a\u043e\u043c 2002-\u043e\u043c \u043a\u043e\u043c\u0438\u0442\u0435\u0442 \u043f\u043e \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u0438\u0437\u0430\u0446\u0438\u0438 C++ \u043f\u043e\u0441\u0435\u0442\u0438\u043b \u043f\u0440\u043e\u043f\u043e\u0437\u0430\u043b, \u043f\u0440\u0435\u0434\u043b\u0430\u0433\u0430\u0432\u0448\u0438\u0439 \u0432\u0432\u0435\u0441\u0442\u0438 \u0448\u0430\u0431\u043b\u043e\u043d\u043d\u044b\u0439 \u043a\u043b\u0430\u0441\u0441, \u043d\u0435\u043a\u0438\u0439 \u043e\u0431\u043e\u0431\u0449\u0435\u043d\u043d\u044b\u0439 \u00ab\u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u043d\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u044e\u00bb, \u0441\u043f\u043e\u0441\u043e\u0431\u043d\u044b\u0439 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u043a\u0430\u043a \u0441 \u043f\u0440\u043e\u0441\u0442\u044b\u043c\u0438 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f\u043c\u0438 \u043d\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f\u043c\u0438 \u043d\u0430 \u043c\u0435\u0442\u043e\u0434\u044b \u043a\u043b\u0430\u0441\u0441\u043e\u0432, \u0442\u0430\u043a \u0438 \u0441 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u043b\u044c\u043d\u044b\u043c\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u044b\u043c\u0438 \u043e\u0431\u044a\u0435\u043a\u0442\u0430\u043c\u0438 <a href=\"https:\/\/wg21.link\/n1402\" rel=\"noopener noreferrer nofollow\">[1]<\/a>.<\/p>\n<p>\u0412 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043c\u043e\u0442\u0438\u0432\u0430\u0446\u0438\u0438 \u043a \u043f\u0440\u0438\u043d\u044f\u0442\u0438\u044e \u043e\u043d \u043f\u0440\u0438\u0432\u043e\u0434\u0438\u043b \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0432\u0435\u0441\u043e\u043c\u044b\u0445 \u044e\u0437\u043a\u0435\u0439\u0441\u043e\u0432: \u043a\u043e\u043b\u0431\u044d\u043a\u0438 \u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0432\u044b\u0441\u0448\u0438\u0445 \u043f\u043e\u0440\u044f\u0434\u043a\u043e\u0432.<\/p>\n<h3>\u041a\u043e\u043b\u0431\u044d\u043a\u0438<\/h3>\n<p><strong>\u0411\u0435\u0437 function<\/strong><\/p>\n<pre><code class=\"cpp\">class mouse_move_listener { public:   virtual void on_mouse_move(int x, int y) = 0; };  class mouse_loc_printer : public mouse_move_listener { public:   virtual void on_mouse_move(int x, int y) {     std::cout &lt;&lt; '(' &lt;&lt; x &lt;&lt; \", \" &lt;&lt; y &lt;&lt; \"\\n\";   } };  mouse_move_listener* move_listener;  void fire_mouse_move(int x, int y) {   if (move_listener)     move_listener->on_mouse_move(x, y); }<\/code><\/pre>\n<p><strong>\u0421 function<\/strong><\/p>\n<pre><code class=\"cpp\">std::function&lt;void (int x, int y)> on_mouse_move;  void fire_mouse_move(int x, int y) {   if (on_mouse_move)     on_mouse_move(x, y); }  on_mouse_move = [](int x, int y) {   std::cout &lt;&lt; '(' &lt;&lt; x &lt;&lt; \", \" &lt;&lt; y &lt;&lt; \"\\n\"; }<\/code><\/pre>\n<h3>\u0424\u0443\u043d\u043a\u0446\u0438\u0438 \u0432\u044b\u0441\u0448\u0438\u0445 \u043f\u043e\u0440\u044f\u0434\u043a\u043e\u0432<\/h3>\n<pre><code class=\"cpp\">function&lt;int (int x, int y)> arithmetic_operation(char k) {   switch (k) {     case '+': return plus&lt;int>();     case '-': return minus&lt;int>();     case '*': return multiplies&lt;int>();     case '\/': return divides&lt;int>();     case '%': return modulus&lt;int>();     default: assert(0);   } }<\/code><\/pre>\n<p>\u041a\u043e\u043c\u0438\u0442\u0435\u0442 \u0434\u043e\u043b\u0433\u043e \u0434\u0443\u043c\u0430\u043b \u0438 \u0440\u0435\u0448\u0438\u043b: \u0444\u0438\u0447\u0435 \u0431\u044b\u0442\u044c! \u0422\u0430\u043a \u0432 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u0443\u044e \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0443 C++11 \u0432\u043e\u0448\u0435\u043b \u0432\u0441\u0435\u043c \u043d\u0430\u043c \u0445\u043e\u0440\u043e\u0448\u043e \u0437\u043d\u0430\u043a\u043e\u043c\u044b\u0439 \u043a\u043b\u0430\u0441\u0441 <code>function<\/code>.<\/p>\n<p>\u0418 \u0432\u0441\u0451 \u0431\u044b\u043b\u043e \u0431\u044b \u0445\u043e\u0440\u043e\u0448\u043e. \u041d\u043e \u044d\u0442\u043e\u0442 \u043a\u043b\u0430\u0441\u0441 \u0432\u044b\u0448\u0435\u043b \u0432 \u0441\u0432\u0435\u0442 \u043d\u0435\u0430\u0434\u0430\u043f\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u043c \u043a \u043d\u0430\u0441\u0442\u0443\u043f\u0438\u0432\u0448\u0438\u043c \u0440\u0435\u0430\u043b\u0438\u044f\u043c.<\/p>\n<p>\u0427\u0442\u043e \u0435\u0441\u043b\u0438 \u043c\u044b \u0437\u0430\u0445\u043e\u0442\u0438\u043c \u0437\u0430\u0445\u0432\u0430\u0442\u0438\u0442\u044c \u0432 \u043b\u044f\u043c\u0431\u0434\u0443 \u0447\u0442\u043e-\u043d\u0438\u0431\u0443\u0434\u044c \u043d\u0435\u043a\u043e\u043f\u0438\u0440\u0443\u0435\u043c\u043e\u0435 \u0438 \u043b\u0438\u0448\u044c \u043f\u0435\u0440\u0435\u043c\u0435\u0449\u0430\u0435\u043c\u043e\u0435? \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, <code>unique_ptr<\/code>? (<a href=\"https:\/\/godbolt.org\/z\/rK76fodj4\" rel=\"noopener noreferrer nofollow\">godbolt<\/a>)<\/p>\n<pre><code class=\"cpp\">#include &lt;functional> #include &lt;memory>  struct widget { };  int main() {   auto w_ptr = std::make_unique&lt;widget>();   std::function&lt;void()> f = [w_ptr = std::move(w_ptr)] { \/*...*\/ }; }<\/code><\/pre>\n<p>\u042d\u0442\u043e\u0442 \u043a\u043e\u0434 \u043f\u0440\u043e\u0441\u0442\u043e \u043d\u0435 \u0441\u043a\u043e\u043c\u043f\u0438\u043b\u0438\u0440\u0443\u0435\u0442\u0441\u044f:<\/p>\n<pre><code class=\"cpp\">\/opt\/...\/function.h:439:69: error: std::function target must be copy-constructible   439 | static_assert(is_copy_constructible&lt;__decay_t&lt;_Functor>>::value,       |                                                           ^~~~~<\/code><\/pre>\n<p>\u0422\u0430\u043a \u043f\u043e\u044f\u0432\u0438\u043b\u0430\u0441\u044c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u044c \u0432 \u043d\u043e\u0432\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e\u0439 \u043e\u0431\u0435\u0440\u0442\u043a\u0435, \u043d\u0430 \u044d\u0442\u043e\u0442 \u0440\u0430\u0437 \u0441\u043f\u043e\u0441\u043e\u0431\u043d\u043e\u0439 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0441 \u043d\u0435\u043a\u043e\u043f\u0438\u0440\u0443\u0435\u043c\u044b\u043c\u0438 \u0442\u0438\u043f\u0430\u043c\u0438. \u0418 \u043a\u043e\u043c\u0438\u0442\u0435\u0442 \u043f\u043e \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u0438\u0437\u0430\u0446\u0438\u0438, \u0442\u0435\u043f\u0435\u0440\u044c \u0432 2015-\u043e\u043c, \u043f\u043e\u0441\u0435\u0442\u0438\u043b \u043d\u043e\u0432\u044b\u0439 \u043f\u0440\u043e\u043f\u043e\u0437\u0430\u043b <a href=\"https:\/\/www.open-std.org\/jtc1\/sc22\/wg21\/docs\/papers\/2015\/n4543.pdf\" rel=\"noopener noreferrer nofollow\">[2]<\/a>&#8230;<\/p>\n<h2>A polymorphic wrapper for all Callable objects<\/h2>\n<p>\u041e\u043d \u043f\u0440\u0435\u0434\u043b\u0430\u0433\u0430\u043b \u0432\u0432\u0435\u0441\u0442\u0438 <code>unique_function<\/code> \u2014 \u0432\u0430\u0440\u0438\u0430\u043d\u0442 <code>function<\/code>, \u043d\u043e \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u044e\u0449\u0438\u0439 \u043d\u0435\u043a\u043e\u043f\u0438\u0440\u0443\u0435\u043c\u044b\u0435 \u0442\u0438\u043f\u044b. \u0414\u0435\u043b\u043e \u0448\u043b\u043e \u0431\u0435\u0437 \u0441\u043f\u0435\u0448\u043a\u0438 \u0438 \u0432 \u0438\u0442\u043e\u0433\u0435 \u044d\u0442\u043e\u0442 \u043f\u0440\u043e\u043f\u043e\u0437\u0430\u043b \u0432 \u0435\u0433\u043e \u043a\u043e\u043d\u0435\u0447\u043d\u043e\u0439 \u0440\u0435\u0434\u0430\u043a\u0446\u0438\u0438 <a href=\"https:\/\/wg21.link\/p0288\" rel=\"noopener noreferrer nofollow\">[3]<\/a> \u0432\u043a\u043b\u044e\u0447\u0438\u043b\u0438 \u0442\u043e\u043b\u044c\u043a\u043e \u0432 C++23 \u043f\u043e\u0434 \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u0435\u043c <code>move_only_function<\/code>. \u0418, \u043c\u0430\u043b\u043e \u0442\u043e\u0433\u043e, \u0447\u0442\u043e \u043e\u043d \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u043b \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0441 \u043d\u0435\u043a\u043e\u043f\u0438\u0440\u0443\u0435\u043c\u044b\u043c\u0438 \u0442\u0438\u043f\u0430\u043c\u0438, \u0432 \u043d\u0435\u043c \u0431\u044b\u043b\u0438 \u0438\u0441\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u044b \u043e\u0431\u043d\u0430\u0440\u0443\u0436\u0435\u043d\u043d\u044b\u0435 \u0437\u0430 \u043f\u0440\u043e\u0448\u0435\u0434\u0448\u0435\u0435 \u0434\u0435\u0441\u044f\u0442\u0438\u043b\u0435\u0442\u0438\u0435 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b <code>function<\/code>:<\/p>\n<ol>\n<li>\n<p>\u0411\u0435\u0434\u044b \u0441 <code>const<\/code>-\u043a\u043e\u0440\u0440\u0435\u043a\u0442\u043d\u043e\u0441\u0442\u044c\u044e.<\/p>\n<\/li>\n<li>\n<p>\u041e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0438\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0438 <code>cv<\/code>\/<code>ref<\/code>\/<code>noexcept<\/code>-\u043a\u0432\u0430\u043b\u0438\u0444\u0438\u0446\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0445 \u0442\u0438\u043f\u043e\u0432.<\/p>\n<\/li>\n<\/ol>\n<p>\u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043a\u0430\u0436\u0434\u0443\u044e \u0438\u0437 \u043d\u0438\u0445 \u043f\u043e\u043f\u043e\u0434\u0440\u043e\u0431\u043d\u0435\u0435 \u043d\u0430 \u043f\u0440\u0438\u043c\u0435\u0440\u0430\u0445.<\/p>\n<h3>\u0411\u0435\u0434\u044b \u0441 const-\u043a\u043e\u0440\u0440\u0435\u043a\u0442\u043d\u043e\u0441\u0442\u044c\u044e<\/h3>\n<p><a href=\"https:\/\/godbolt.org\/z\/987z7PYnT\" rel=\"noopener noreferrer nofollow\">\u041f\u0440\u0438\u043c\u0435\u0440 \u0434\u043e\u0441\u0442\u0443\u043f\u0435\u043d \u043d\u0430 godbolt<\/a><\/p>\n<pre><code class=\"cpp\">#include &lt;functional>  int main() {     \/\/ \u0414\u043e\u043f\u0443\u0441\u0442\u0438\u043c, \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c function \u043e\u0431\u044a\u0435\u043a\u0442, \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439     \/\/ \u043c\u0443\u0442\u0430\u0431\u0435\u043b\u044c\u043d\u043e\u0439 \u043b\u044f\u043c\u0431\u0434\u043e\u0439:     std::function&lt;void(void)> func{[&amp;]() mutable {     \/\/ ...     }};     \/\/ \u041c\u044b \u043c\u043e\u0436\u0435\u043c \u0435\u0433\u043e \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c \u0438 \u0432\u0441\u0451 \u0445\u043e\u0440\u043e\u0448\u043e:     func();      \/\/ \u041d\u043e \u0435\u0441\u043b\u0438 \u043d\u0430\u043c \u0437\u0430\u0445\u043e\u0447\u0435\u0442\u0441\u044f \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u043a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u043d\u0443\u044e \u0441\u0441\u044b\u043b\u043a\u0443 \u043d\u0430 \u043d\u0435\u0433\u043e,     \/\/ \u0440\u0430\u0441\u0441\u0447\u0438\u0442\u044b\u0432\u0430\u044f, \u0447\u0442\u043e \u0443 \u043d\u0430\u0441 \u043d\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u0441\u044f \u0432\u044b\u0437\u0432\u0430\u0442\u044c function,     \/\/ \u0435\u0441\u043b\u0438 \u043e\u0431\u044a\u0435\u043a\u0442, \u0437\u0430\u0445\u0432\u0430\u0447\u0435\u043d\u043d\u044b\u0439 \u0432 \u043d\u0435\u0433\u043e, \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0438\u0437\u043c\u0435\u043d\u044f\u0435\u043c\u044b\u043c     const auto &amp;ref{func};     \/\/ \u0422\u043e \u043c\u044b \u043d\u0435 \u0434\u043e\u0441\u0442\u0438\u0433\u043d\u0435\u043c \u0441\u0432\u043e\u0438\u0445 \u0446\u0435\u043b\u0435\u0439! \u041a\u043e\u0434 \u043d\u0438\u0436\u0435 \u0441\u043a\u043e\u043c\u043f\u0438\u043b\u0438\u0440\u0443\u0435\u0442\u0441\u044f!     \/\/ const - \u043d\u0435 const \u2014 \u0434\u043b\u044f function \u044d\u0442\u043e \u043d\u0438\u0447\u0435\u0433\u043e \u043d\u0435 \u0437\u043d\u0430\u0447\u0438\u0442     ref(); }<\/code><\/pre>\n<h3>\u041e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0438\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0438 cv\/ref\/noexcept-\u043a\u0432\u0430\u043b\u0438\u0444\u0438\u0446\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0445 \u0442\u0438\u043f\u043e\u0432<\/h3>\n<p><a href=\"https:\/\/godbolt.org\/z\/bEhxfnMPo\" rel=\"noopener noreferrer nofollow\">\u041f\u0440\u0438\u043c\u0435\u0440 \u0434\u043e\u0441\u0442\u0443\u043f\u0435\u043d \u043d\u0430 godbolt<\/a><\/p>\n<pre><code class=\"cpp\">#include &lt;functional>  void f() noexcept { }  int main() {   \/\/ \u2014 \u0427\u0442\u043e-\u0447\u0442\u043e? \u0421\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c noexcept \u0441\u043f\u0435\u0446\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440?   \/\/ \u041d\u0435\u0442, \u043d\u0430\u0441 \u0434\u0438\u0437\u0430\u0439\u043d\u0438\u043b\u0438 \u0432 2002 \u0433\u043e\u0434\u0443! \u041f\u043e\u043b\u0443\u0447\u0430\u0439 \u043e\u0448\u0438\u0431\u043a\u0443 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0446\u0438\u0438!     std::function&lt;void() noexcept> fnp = f;      \/\/ \u2014 \u041d\u0443 \u0434\u0430\u0432\u0430\u0439 \u0445\u043e\u0442\u044f \u0431\u044b const?   \/\/ \u2014 \u041e\u0442\u0432\u0430\u043b\u0438. Compiler Error     std::function&lt;void() const> fcp = []() const {};      \/\/ ... }<\/code><\/pre>\n<p>\u0418 \u0432\u0441\u0435 \u0431\u044b \u0445\u043e\u0440\u043e\u0448\u043e, \u043d\u043e&#8230;<\/p>\n<h2>Copyable function<\/h2>\n<p>\u00ab\u041f\u043e\u0434\u043e\u0436\u0434\u0438\u0442\u0435! \u2014 \u0432\u043e\u0441\u043a\u043b\u0438\u043a\u043d\u0443\u043b \u0432\u043d\u0438\u043c\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u0447\u0438\u0442\u0430\u0442\u0435\u043b\u044c, \u2014 \u0432\u043e\u0442 \u044d\u0442\u0438 \u0432\u0441\u0435 \u043a\u043e\u0441\u044f\u043a\u0438, \u0430 \u043f\u043e\u0447\u0435\u043c\u0443 \u0431\u044b \u0438\u0445 \u043d\u0435 \u0438\u0441\u043f\u0440\u0430\u0432\u0438\u0442\u044c \u0432 \u043e\u0431\u044b\u0447\u043d\u043e\u043c <code>function<\/code>, \u043d\u0435 \u0432\u0435\u0447\u043d\u043e\u0441\u0442\u044c \u0436\u0435 \u0442\u0435\u043f\u0435\u0440\u044c \u0436\u0438\u0442\u044c \u0441 \u043d\u0438\u043c\u0438, \u0438\u043c\u0435\u044f \u043e\u0434\u0438\u043d <code>move_only_function<\/code>, \u0433\u0434\u0435 \u043e\u043d\u0438 \u0438\u0441\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u044b, \u0438 \u043d\u0435 \u0438\u043c\u0435\u044f \u043a\u043e\u043f\u0438\u0440\u0443\u0435\u043c\u044b\u0439 \u0430\u043d\u0430\u043b\u043e\u0433 <code>move_only_function<\/code>?<\/p>\n<p>\u041a\u043e\u043c\u0438\u0442\u0435\u0442 \u043f\u043e\u0434\u0443\u043c\u0430\u043b \u0438 \u0440\u0435\u0448\u0438\u043b: \u00ab\u0412 \u044d\u0442\u0438\u0445 \u0441\u043b\u043e\u0432\u0430\u0445 \u0435\u0441\u0442\u044c \u0441\u043c\u044b\u0441\u043b, \u043d\u043e \u043c\u044b \u043d\u0435 \u0433\u043e\u0442\u043e\u0432\u044b \u0438\u0441\u043f\u0440\u0430\u0432\u0438\u0442\u044c \u0441\u0430\u043c <code>function<\/code>. \u041c\u044b \u043d\u0435 \u0433\u043e\u0442\u043e\u0432\u044b \u043d\u0430\u0440\u0443\u0448\u0438\u0442\u044c \u0443\u0441\u0442\u043e\u0438 \u0438 \u0442\u0440\u0430\u0434\u0438\u0446\u0438\u0438, \u043d\u0430 \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0441 2011-\u0433\u043e \u0433\u043e\u0434\u0430 \u043f\u043e\u043b\u0430\u0433\u0430\u044e\u0442\u0441\u044f \u0442\u044b\u0441\u044f\u0447\u0438 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0441\u0442\u043e\u0432\u00bb.<\/p>\n<p>\u0412\u043d\u0438\u043c\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u0447\u0438\u0442\u0430\u0442\u0435\u043b\u044c \u043a\u0438\u0432\u043d\u0443\u043b, \u0443\u0434\u0430\u043b\u044f\u0441\u044c \u0434\u043b\u044f \u0434\u0443\u043c \u043f\u0440\u043e\u0447\u044c. \u041d\u043e \u0432\u0441\u043a\u043e\u0440\u0435 \u0432\u0435\u0440\u043d\u0443\u043b\u0441\u044f \u0441 \u043f\u0440\u043e\u043f\u043e\u0437\u0430\u043b\u043e\u043c, \u043f\u0440\u0435\u0434\u043b\u0430\u0433\u0430\u0432\u0448\u0438\u043c \u0432\u0432\u0435\u0441\u0442\u0438 \u043d\u043e\u0432\u044b\u0439 \u043a\u043b\u0430\u0441\u0441, \u0432\u0430\u0440\u0438\u0430\u043d\u0442 <code>move_only_function<\/code>, \u043d\u043e \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u044e\u0449\u0438\u0439 \u043a\u043e\u043f\u0438\u0440\u0443\u0435\u043c\u044b\u0435 \u0442\u0438\u043f\u044b. \u0422\u0430\u043a \u0432 C++26 \u043f\u0440\u0438\u043d\u044f\u043b\u0438 <code>copyable_function<\/code> [<a href=\"https:\/\/wg21.link\/p2548\" rel=\"noopener noreferrer nofollow\">4<\/a>].<\/p>\n<h3>\u041f\u0435\u0440\u0432\u044b\u0439 \u043f\u0440\u0438\u043c\u0435\u0440<\/h3>\n<pre><code class=\"cpp\">auto lambda{[&amp;]() \/*const*\/ { \u2026 }};  copyable_function&lt;void(void)> func0{lambda}; const auto &amp; ref0{func0};  \/\/ \u0412\u0441\u0435 \u0445\u043e\u0440\u043e\u0448\u043e, \u043c\u044b \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u043c \/\/ \u043d\u0435\u043a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u043d\u0443\u044e (\u043f\u043e \u043f\u0435\u0440\u0435\u0434\u0430\u043d\u043d\u043e\u0439 \u0441\u0438\u0433\u043d\u0430\u0442\u0443\u0440\u0435) \u0444\u0443\u043d\u043a\u0446\u0438\u044e \/\/ \u0447\u0435\u0440\u0435\u0437 \u043d\u0435\u043a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u043d\u044b\u0439 \u043e\u0431\u044a\u0435\u043a\u0442 func0(); \/\/ Compilation error: \u043f\u044b\u0442\u0430\u0435\u043c\u0441\u044f \u0432\u044b\u0437\u0432\u0430\u0442\u044c \/\/ \u043d\u0435\u043a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u043d\u0443\u044e (\u043f\u043e \u043f\u0435\u0440\u0435\u0434\u0430\u043d\u043d\u043e\u0439 \u0441\u0438\u0433\u043d\u0430\u0442\u0443\u0440\u0435) \u0444\u0443\u043d\u043a\u0446\u0438\u044e \/\/ \u0447\u0435\u0440\u0435\u0437 \u043a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u043d\u044b\u0439 \u043e\u0431\u044a\u0435\u043a\u0442 ref0(); \/\/ operator() is NOT const!  \/\/ ---  copyable_function&lt;void(void) const> func1{lambda}; const auto &amp; ref1{func1};  \/\/ All is OK. \u0412\u044b\u0437\u044b\u0432\u0430\u0435\u043c \u043a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u043d\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e \/\/ 1) \u0427\u0435\u0440\u0435\u0437 \u043d\u0435\u043a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u043d\u044b\u0439 \u043e\u0431\u044a\u0435\u043a\u0442 func1(); \/\/ 2) \u0427\u0435\u0440\u0435\u0437 \u043a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u043d\u044b\u0439 \u043e\u0431\u044a\u0435\u043a\u0442 ref1(); \/\/ operator() is const!<\/code><\/pre>\n<h3>\u0412\u0442\u043e\u0440\u043e\u0439 \u043f\u0440\u0438\u043c\u0435\u0440<\/h3>\n<pre><code class=\"cpp\">auto lambda{[&amp;]() mutable { \u2026 }};  copyable_function&lt;void(void)> func{lambda}; const auto &amp; ref{func};  \/\/ \u0412\u0441\u0435 \u0445\u043e\u0440\u043e\u0448\u043e: \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u043c \/\/ \u043d\u0435\u043a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u043d\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0447\u0435\u0440\u0435\u0437 \u043d\u0435\u043a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u043d\u044b\u0439 \u043e\u0431\u044a\u0435\u043a\u0442 func(); \/\/ Compilation error: \u043f\u044b\u0442\u0430\u0435\u043c\u0441\u044f \u0432\u044b\u0437\u0432\u0430\u0442\u044c \/\/ \u043d\u0435\u043a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u043d\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0447\u0435\u0440\u0435\u0437 \u043a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u043d\u044b\u0439 \u043e\u0431\u044a\u0435\u043a\u0442 ref();  \/\/ Compilation error: \u043d\u0435 \u043c\u043e\u0436\u0435\u043c \/\/ \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u043d\u0435\u043a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u043d\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u043a\u0430\u043a \u043a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u043d\u0443\u044e copyable_function&lt;void(void) const> tmp{lambda};<\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c-\u0442\u043e \u0442\u043e\u0447\u043d\u043e \u0432\u0441\u0435 \u0434\u043e\u043b\u0436\u043d\u044b \u0431\u044b\u0442\u044c \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u044b? \ud83d\ude42<\/p>\n<p>\u041d\u0435 \u0441\u043a\u0430\u0436\u0438\u0442\u0435! \u0427\u0442\u043e \u043c\u044b \u0443\u043f\u0443\u0441\u0442\u0438\u043b\u0438 \u2014 \u0438 <code>function<\/code>, \u0438 <code>move_only_function<\/code>, \u0438 <code>copyable_function<\/code> \u2014 \u0432\u0441\u0435 \u043e\u043d\u0438 \u043f\u043e \u0441\u0432\u043e\u0435\u0439 \u0441\u0435\u043c\u0430\u043d\u0442\u0438\u043a\u0435 <strong>\u0432\u043b\u0430\u0434\u0435\u044e\u0442<\/strong> \u0437\u0430\u0432\u0435\u0440\u043d\u0443\u0442\u044b\u043c\u0438 \u0432 \u043d\u0438\u0445 \u0444\u0443\u043d\u043a\u0446\u0438\u044f\u043c\u0438. \u0410 \u0447\u0442\u043e \u0435\u0441\u043b\u0438, \u0435\u0441\u043b\u0438 \u0432\u0441\u0435, \u0447\u0442\u043e \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u2014 \u044d\u0442\u043e <strong>\u043d\u0435\u0432\u043b\u0430\u0434\u0435\u044e\u0449\u0438\u0439<\/strong> reference \u043d\u0430 callable \u043e\u0431\u044a\u0435\u043a\u0442? \u041a\u043e\u0442\u043e\u0440\u044b\u0439, \u043c\u0435\u0436\u0434\u0443 \u043f\u0440\u043e\u0447\u0438\u043c, \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0434\u0430\u0436\u0435 \u043d\u0438 \u043a\u043e\u043f\u0438\u0440\u0443\u0435\u043c\u044b\u043c, \u043d\u0438 \u043f\u0435\u0440\u0435\u043c\u0435\u0449\u0430\u0435\u043c\u044b\u043c.<\/p>\n<h2>Non-owning reference to a Callable<\/h2>\n<p>\u0412\u0441\u0442\u0440\u0435\u0447\u0430\u0439\u0442\u0435 <code>function_ref<\/code>, \u043f\u0440\u0438\u043d\u044f\u0442\u044b\u0439 \u0432 C++26 [<a href=\"https:\/\/wg21.link\/P0792\" rel=\"noopener noreferrer nofollow\">5<\/a>]!<\/p>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c \u0442\u043e, \u0447\u0442\u043e \u043d\u0435\u043b\u044c\u0437\u044f \u043d\u0438 \u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c, \u043d\u0438 \u043f\u0435\u0440\u0435\u043c\u0435\u0449\u0430\u0442\u044c:<\/p>\n<pre><code class=\"cpp\">#include &lt;functional>  struct A {     A() { }     A(const A&amp;) = delete;     A(A&amp;&amp;) = delete;     void operator()() { } };  int main() {     A obj;      \/\/ Compile errors:     std::function&lt;void(void)> func1 = obj;     std::move_only_function&lt;void(void)> func2 = std::move(obj);     std::copyable_function&lt;void(void)> func3 = obj;      \/\/ All is OK:     std::function_ref&lt;void(void)> func4 = obj;     func4(); }<\/code><\/pre>\n<p>\u0418, \u0432\u0441\u043f\u043e\u043c\u0438\u043d\u0430\u044f \u043e \u043a\u043e\u043b\u0431\u044d\u043a\u0430\u0445, \u043f\u043e\u0441\u043b\u0443\u0436\u0438\u0432\u0448\u0438\u0445 \u043e\u0434\u043d\u0438\u043c \u0438\u0437 \u0432\u0435\u0434\u0443\u0449\u0438\u0445 \u044e\u0437\u043a\u0435\u0439\u0441\u043e\u0432, \u0434\u0435\u043c\u043e\u043d\u0441\u0442\u0440\u0438\u0440\u0443\u044e\u0449\u0438\u0445 \u043f\u043e\u043b\u0435\u0437\u043d\u043e\u0441\u0442\u044c <code>function<\/code>. \u0410 \u0440\u0430\u0437\u0432\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u044f, \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u044e\u0449\u0430\u044f \u043a\u043e\u043b\u0431\u044d\u043a, \u0436\u0435\u043b\u0430\u0435\u0442 \u0432\u043b\u0430\u0434\u0435\u0442\u044c \u0438\u043c? \u0416\u0435\u043b\u0430\u0435\u0442 \u043e\u0432\u0435\u0440\u0445\u0435\u0434 \u0432 \u0432\u0438\u0434\u0435 \u0430\u043b\u043b\u043e\u043a\u0430\u0446\u0438\u0439? \u0412 \u0431\u043e\u043b\u044c\u0448\u0438\u043d\u0441\u0442\u0432\u0435 \u0441\u043b\u0443\u0447\u0430\u0435\u0432 \u2014 \u043d\u0435\u0442. <\/p>\n<p>\u0422\u0430\u043a \u0447\u0442\u043e, \u043d\u0430 \u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435, \u044d\u0442\u043e \u0431\u043e\u043b\u044c\u0448\u0435 \u044e\u0437\u043a\u0435\u0439\u0441 \u0434\u043b\u044f <code>function_ref<\/code>:<\/p>\n<pre><code class=\"cpp\">data retry(size_t times, function_ref&lt;data()> action) { \/\/ ... }<\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0442\u043e \u043d\u0430\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u044b\u0445 \u043e\u0431\u0435\u0440\u0442\u043e\u043a \u0445\u0432\u0430\u0442\u0438\u0442? <strong>\u0418\u043b\u0438, \u043c\u043e\u0436\u0435\u0442, \u0441\u0430\u043c\u043e\u0435 \u0432\u0440\u0435\u043c\u044f \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043f\u0440\u043e\u043f\u043e\u0437\u0430\u043b, \u043f\u0440\u0435\u0434\u043b\u0430\u0433\u0430\u044e\u0449\u0438\u0439 \u043a\u0430\u043a\u0443\u044e-\u043d\u0438\u0431\u0443\u0434\u044c \u0435\u0449\u0435?<\/strong> \ud83d\ude42<\/p>\n<blockquote>\n<p>\u041e\u043f\u0443\u0431\u043b\u0438\u043a\u043e\u0432\u0430\u043d\u043e \u043f\u0440\u0438 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0435 <a href=\"https:\/\/t.me\/cppmoscow_info\" rel=\"noopener noreferrer nofollow\">C++ Moscow<\/a><\/p>\n<\/blockquote>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \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\/articles\/788524\/\"> https:\/\/habr.com\/ru\/articles\/788524\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<h2>A polymorphic function object wrapper<\/h2>\n<p>\u0412 \u0434\u0430\u043b\u0451\u043a\u043e\u043c 2002-\u043e\u043c \u043a\u043e\u043c\u0438\u0442\u0435\u0442 \u043f\u043e \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u0438\u0437\u0430\u0446\u0438\u0438 C++ \u043f\u043e\u0441\u0435\u0442\u0438\u043b \u043f\u0440\u043e\u043f\u043e\u0437\u0430\u043b, \u043f\u0440\u0435\u0434\u043b\u0430\u0433\u0430\u0432\u0448\u0438\u0439 \u0432\u0432\u0435\u0441\u0442\u0438 \u0448\u0430\u0431\u043b\u043e\u043d\u043d\u044b\u0439 \u043a\u043b\u0430\u0441\u0441, \u043d\u0435\u043a\u0438\u0439 \u043e\u0431\u043e\u0431\u0449\u0435\u043d\u043d\u044b\u0439 \u00ab\u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u043d\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u044e\u00bb, \u0441\u043f\u043e\u0441\u043e\u0431\u043d\u044b\u0439 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u043a\u0430\u043a \u0441 \u043f\u0440\u043e\u0441\u0442\u044b\u043c\u0438 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f\u043c\u0438 \u043d\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f\u043c\u0438 \u043d\u0430 \u043c\u0435\u0442\u043e\u0434\u044b \u043a\u043b\u0430\u0441\u0441\u043e\u0432, \u0442\u0430\u043a \u0438 \u0441 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u043b\u044c\u043d\u044b\u043c\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u044b\u043c\u0438 \u043e\u0431\u044a\u0435\u043a\u0442\u0430\u043c\u0438 <a href=\"https:\/\/wg21.link\/n1402\" rel=\"noopener noreferrer nofollow\">[1]<\/a>.<\/p>\n<p>\u0412 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043c\u043e\u0442\u0438\u0432\u0430\u0446\u0438\u0438 \u043a \u043f\u0440\u0438\u043d\u044f\u0442\u0438\u044e \u043e\u043d \u043f\u0440\u0438\u0432\u043e\u0434\u0438\u043b \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0432\u0435\u0441\u043e\u043c\u044b\u0445 \u044e\u0437\u043a\u0435\u0439\u0441\u043e\u0432: \u043a\u043e\u043b\u0431\u044d\u043a\u0438 \u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0432\u044b\u0441\u0448\u0438\u0445 \u043f\u043e\u0440\u044f\u0434\u043a\u043e\u0432.<\/p>\n<h3>\u041a\u043e\u043b\u0431\u044d\u043a\u0438<\/h3>\n<p><strong>\u0411\u0435\u0437 function<\/strong><\/p>\n<pre><code class=\"cpp\">class mouse_move_listener { public:   virtual void on_mouse_move(int x, int y) = 0; };  class mouse_loc_printer : public mouse_move_listener { public:   virtual void on_mouse_move(int x, int y) {     std::cout &lt;&lt; '(' &lt;&lt; x &lt;&lt; \", \" &lt;&lt; y &lt;&lt; \"\\n\";   } };  mouse_move_listener* move_listener;  void fire_mouse_move(int x, int y) {   if (move_listener)     move_listener->on_mouse_move(x, y); }<\/code><\/pre>\n<p><strong>\u0421 function<\/strong><\/p>\n<pre><code class=\"cpp\">std::function&lt;void (int x, int y)> on_mouse_move;  void fire_mouse_move(int x, int y) {   if (on_mouse_move)     on_mouse_move(x, y); }  on_mouse_move = [](int x, int y) {   std::cout &lt;&lt; '(' &lt;&lt; x &lt;&lt; \", \" &lt;&lt; y &lt;&lt; \"\\n\"; }<\/code><\/pre>\n<h3>\u0424\u0443\u043d\u043a\u0446\u0438\u0438 \u0432\u044b\u0441\u0448\u0438\u0445 \u043f\u043e\u0440\u044f\u0434\u043a\u043e\u0432<\/h3>\n<pre><code class=\"cpp\">function&lt;int (int x, int y)> arithmetic_operation(char k) {   switch (k) {     case '+': return plus&lt;int>();     case '-': return minus&lt;int>();     case '*': return multiplies&lt;int>();     case '\/': return divides&lt;int>();     case '%': return modulus&lt;int>();     default: assert(0);   } }<\/code><\/pre>\n<p>\u041a\u043e\u043c\u0438\u0442\u0435\u0442 \u0434\u043e\u043b\u0433\u043e \u0434\u0443\u043c\u0430\u043b \u0438 \u0440\u0435\u0448\u0438\u043b: \u0444\u0438\u0447\u0435 \u0431\u044b\u0442\u044c! \u0422\u0430\u043a \u0432 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u0443\u044e \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0443 C++11 \u0432\u043e\u0448\u0435\u043b \u0432\u0441\u0435\u043c \u043d\u0430\u043c \u0445\u043e\u0440\u043e\u0448\u043e \u0437\u043d\u0430\u043a\u043e\u043c\u044b\u0439 \u043a\u043b\u0430\u0441\u0441 <code>function<\/code>.<\/p>\n<p>\u0418 \u0432\u0441\u0451 \u0431\u044b\u043b\u043e \u0431\u044b \u0445\u043e\u0440\u043e\u0448\u043e. \u041d\u043e \u044d\u0442\u043e\u0442 \u043a\u043b\u0430\u0441\u0441 \u0432\u044b\u0448\u0435\u043b \u0432 \u0441\u0432\u0435\u0442 \u043d\u0435\u0430\u0434\u0430\u043f\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u043c \u043a \u043d\u0430\u0441\u0442\u0443\u043f\u0438\u0432\u0448\u0438\u043c \u0440\u0435\u0430\u043b\u0438\u044f\u043c.<\/p>\n<p>\u0427\u0442\u043e \u0435\u0441\u043b\u0438 \u043c\u044b \u0437\u0430\u0445\u043e\u0442\u0438\u043c \u0437\u0430\u0445\u0432\u0430\u0442\u0438\u0442\u044c \u0432 \u043b\u044f\u043c\u0431\u0434\u0443 \u0447\u0442\u043e-\u043d\u0438\u0431\u0443\u0434\u044c \u043d\u0435\u043a\u043e\u043f\u0438\u0440\u0443\u0435\u043c\u043e\u0435 \u0438 \u043b\u0438\u0448\u044c \u043f\u0435\u0440\u0435\u043c\u0435\u0449\u0430\u0435\u043c\u043e\u0435? \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, <code>unique_ptr<\/code>? (<a href=\"https:\/\/godbolt.org\/z\/rK76fodj4\" rel=\"noopener noreferrer nofollow\">godbolt<\/a>)<\/p>\n<pre><code class=\"cpp\">#include &lt;functional> #include &lt;memory>  struct widget { };  int main() {   auto w_ptr = std::make_unique&lt;widget>();   std::function&lt;void()> f = [w_ptr = std::move(w_ptr)] { \/*...*\/ }; }<\/code><\/pre>\n<p>\u042d\u0442\u043e\u0442 \u043a\u043e\u0434 \u043f\u0440\u043e\u0441\u0442\u043e \u043d\u0435 \u0441\u043a\u043e\u043c\u043f\u0438\u043b\u0438\u0440\u0443\u0435\u0442\u0441\u044f:<\/p>\n<pre><code class=\"cpp\">\/opt\/...\/function.h:439:69: error: std::function target must be copy-constructible   439 | static_assert(is_copy_constructible&lt;__decay_t&lt;_Functor>>::value,       |                                                           ^~~~~<\/code><\/pre>\n<p>\u0422\u0430\u043a \u043f\u043e\u044f\u0432\u0438\u043b\u0430\u0441\u044c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u044c \u0432 \u043d\u043e\u0432\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e\u0439 \u043e\u0431\u0435\u0440\u0442\u043a\u0435, \u043d\u0430 \u044d\u0442\u043e\u0442 \u0440\u0430\u0437 \u0441\u043f\u043e\u0441\u043e\u0431\u043d\u043e\u0439 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0441 \u043d\u0435\u043a\u043e\u043f\u0438\u0440\u0443\u0435\u043c\u044b\u043c\u0438 \u0442\u0438\u043f\u0430\u043c\u0438. \u0418 \u043a\u043e\u043c\u0438\u0442\u0435\u0442 \u043f\u043e \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u0438\u0437\u0430\u0446\u0438\u0438, \u0442\u0435\u043f\u0435\u0440\u044c \u0432 2015-\u043e\u043c, \u043f\u043e\u0441\u0435\u0442\u0438\u043b \u043d\u043e\u0432\u044b\u0439 \u043f\u0440\u043e\u043f\u043e\u0437\u0430\u043b <a href=\"https:\/\/www.open-std.org\/jtc1\/sc22\/wg21\/docs\/papers\/2015\/n4543.pdf\" rel=\"noopener noreferrer nofollow\">[2]<\/a>&#8230;<\/p>\n<h2>A polymorphic wrapper for all Callable objects<\/h2>\n<p>\u041e\u043d \u043f\u0440\u0435\u0434\u043b\u0430\u0433\u0430\u043b \u0432\u0432\u0435\u0441\u0442\u0438 <code>unique_function<\/code> \u2014 \u0432\u0430\u0440\u0438\u0430\u043d\u0442 <code>function<\/code>, \u043d\u043e \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u044e\u0449\u0438\u0439 \u043d\u0435\u043a\u043e\u043f\u0438\u0440\u0443\u0435\u043c\u044b\u0435 \u0442\u0438\u043f\u044b. \u0414\u0435\u043b\u043e \u0448\u043b\u043e \u0431\u0435\u0437 \u0441\u043f\u0435\u0448\u043a\u0438 \u0438 \u0432 \u0438\u0442\u043e\u0433\u0435 \u044d\u0442\u043e\u0442 \u043f\u0440\u043e\u043f\u043e\u0437\u0430\u043b \u0432 \u0435\u0433\u043e \u043a\u043e\u043d\u0435\u0447\u043d\u043e\u0439 \u0440\u0435\u0434\u0430\u043a\u0446\u0438\u0438 <a href=\"https:\/\/wg21.link\/p0288\" rel=\"noopener noreferrer nofollow\">[3]<\/a> \u0432\u043a\u043b\u044e\u0447\u0438\u043b\u0438 \u0442\u043e\u043b\u044c\u043a\u043e \u0432 C++23 \u043f\u043e\u0434 \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u0435\u043c <code>move_only_function<\/code>. \u0418, \u043c\u0430\u043b\u043e \u0442\u043e\u0433\u043e, \u0447\u0442\u043e \u043e\u043d \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u043b \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0441 \u043d\u0435\u043a\u043e\u043f\u0438\u0440\u0443\u0435\u043c\u044b\u043c\u0438 \u0442\u0438\u043f\u0430\u043c\u0438, \u0432 \u043d\u0435\u043c \u0431\u044b\u043b\u0438 \u0438\u0441\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u044b \u043e\u0431\u043d\u0430\u0440\u0443\u0436\u0435\u043d\u043d\u044b\u0435 \u0437\u0430 \u043f\u0440\u043e\u0448\u0435\u0434\u0448\u0435\u0435 \u0434\u0435\u0441\u044f\u0442\u0438\u043b\u0435\u0442\u0438\u0435 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b <code>function<\/code>:<\/p>\n<ol>\n<li>\n<p>\u0411\u0435\u0434\u044b \u0441 <code>const<\/code>-\u043a\u043e\u0440\u0440\u0435\u043a\u0442\u043d\u043e\u0441\u0442\u044c\u044e.<\/p>\n<\/li>\n<li>\n<p>\u041e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0438\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0438 <code>cv<\/code>\/<code>ref<\/code>\/<code>noexcept<\/code>-\u043a\u0432\u0430\u043b\u0438\u0444\u0438\u0446\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0445 \u0442\u0438\u043f\u043e\u0432.<\/p>\n<\/li>\n<\/ol>\n<p>\u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043a\u0430\u0436\u0434\u0443\u044e \u0438\u0437 \u043d\u0438\u0445 \u043f\u043e\u043f\u043e\u0434\u0440\u043e\u0431\u043d\u0435\u0435 \u043d\u0430 \u043f\u0440\u0438\u043c\u0435\u0440\u0430\u0445.<\/p>\n<h3>\u0411\u0435\u0434\u044b \u0441 const-\u043a\u043e\u0440\u0440\u0435\u043a\u0442\u043d\u043e\u0441\u0442\u044c\u044e<\/h3>\n<p><a href=\"https:\/\/godbolt.org\/z\/987z7PYnT\" rel=\"noopener noreferrer nofollow\">\u041f\u0440\u0438\u043c\u0435\u0440 \u0434\u043e\u0441\u0442\u0443\u043f\u0435\u043d \u043d\u0430 godbolt<\/a><\/p>\n<pre><code class=\"cpp\">#include &lt;functional>  int main() {     \/\/ \u0414\u043e\u043f\u0443\u0441\u0442\u0438\u043c, \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c function \u043e\u0431\u044a\u0435\u043a\u0442, \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439     \/\/ \u043c\u0443\u0442\u0430\u0431\u0435\u043b\u044c\u043d\u043e\u0439 \u043b\u044f\u043c\u0431\u0434\u043e\u0439:     std::function&lt;void(void)> func{[&amp;]() mutable {     \/\/ ...     }};     \/\/ \u041c\u044b \u043c\u043e\u0436\u0435\u043c \u0435\u0433\u043e \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c \u0438 \u0432\u0441\u0451 \u0445\u043e\u0440\u043e\u0448\u043e:     func();      \/\/ \u041d\u043e \u0435\u0441\u043b\u0438 \u043d\u0430\u043c \u0437\u0430\u0445\u043e\u0447\u0435\u0442\u0441\u044f \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u043a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u043d\u0443\u044e \u0441\u0441\u044b\u043b\u043a\u0443 \u043d\u0430 \u043d\u0435\u0433\u043e,     \/\/ \u0440\u0430\u0441\u0441\u0447\u0438\u0442\u044b\u0432\u0430\u044f, \u0447\u0442\u043e \u0443 \u043d\u0430\u0441 \u043d\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u0441\u044f \u0432\u044b\u0437\u0432\u0430\u0442\u044c function,     \/\/ \u0435\u0441\u043b\u0438 \u043e\u0431\u044a\u0435\u043a\u0442, \u0437\u0430\u0445\u0432\u0430\u0447\u0435\u043d\u043d\u044b\u0439 \u0432 \u043d\u0435\u0433\u043e, \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0438\u0437\u043c\u0435\u043d\u044f\u0435\u043c\u044b\u043c     const auto &amp;ref{func};     \/\/ \u0422\u043e \u043c\u044b \u043d\u0435 \u0434\u043e\u0441\u0442\u0438\u0433\u043d\u0435\u043c \u0441\u0432\u043e\u0438\u0445 \u0446\u0435\u043b\u0435\u0439! \u041a\u043e\u0434 \u043d\u0438\u0436\u0435 \u0441\u043a\u043e\u043c\u043f\u0438\u043b\u0438\u0440\u0443\u0435\u0442\u0441\u044f!     \/\/ const - \u043d\u0435 const \u2014 \u0434\u043b\u044f function \u044d\u0442\u043e \u043d\u0438\u0447\u0435\u0433\u043e \u043d\u0435 \u0437\u043d\u0430\u0447\u0438\u0442     ref(); }<\/code><\/pre>\n<h3>\u041e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0438\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0438 cv\/ref\/noexcept-\u043a\u0432\u0430\u043b\u0438\u0444\u0438\u0446\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0445 \u0442\u0438\u043f\u043e\u0432<\/h3>\n<p><a href=\"https:\/\/godbolt.org\/z\/bEhxfnMPo\" rel=\"noopener noreferrer nofollow\">\u041f\u0440\u0438\u043c\u0435\u0440 \u0434\u043e\u0441\u0442\u0443\u043f\u0435\u043d \u043d\u0430 godbolt<\/a><\/p>\n<pre><code class=\"cpp\">#include &lt;functional>  void f() noexcept { }  int main() {   \/\/ \u2014 \u0427\u0442\u043e-\u0447\u0442\u043e? \u0421\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c noexcept \u0441\u043f\u0435\u0446\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440?   \/\/ \u041d\u0435\u0442, \u043d\u0430\u0441 \u0434\u0438\u0437\u0430\u0439\u043d\u0438\u043b\u0438 \u0432 2002 \u0433\u043e\u0434\u0443! \u041f\u043e\u043b\u0443\u0447\u0430\u0439 \u043e\u0448\u0438\u0431\u043a\u0443 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0446\u0438\u0438!     std::function&lt;void() noexcept> fnp = f;      \/\/ \u2014 \u041d\u0443 \u0434\u0430\u0432\u0430\u0439 \u0445\u043e\u0442\u044f \u0431\u044b const?   \/\/ \u2014 \u041e\u0442\u0432\u0430\u043b\u0438. Compiler Error     std::function&lt;void() const> fcp = []() const {};      \/\/ ... }<\/code><\/pre>\n<p>\u0418 \u0432\u0441\u0435 \u0431\u044b \u0445\u043e\u0440\u043e\u0448\u043e, \u043d\u043e&#8230;<\/p>\n<h2>Copyable function<\/h2>\n<p>\u00ab\u041f\u043e\u0434\u043e\u0436\u0434\u0438\u0442\u0435! \u2014 \u0432\u043e\u0441\u043a\u043b\u0438\u043a\u043d\u0443\u043b \u0432\u043d\u0438\u043c\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u0447\u0438\u0442\u0430\u0442\u0435\u043b\u044c, \u2014 \u0432\u043e\u0442 \u044d\u0442\u0438 \u0432\u0441\u0435 \u043a\u043e\u0441\u044f\u043a\u0438, \u0430 \u043f\u043e\u0447\u0435\u043c\u0443 \u0431\u044b \u0438\u0445 \u043d\u0435 \u0438\u0441\u043f\u0440\u0430\u0432\u0438\u0442\u044c \u0432 \u043e\u0431\u044b\u0447\u043d\u043e\u043c <code>function<\/code>, \u043d\u0435 \u0432\u0435\u0447\u043d\u043e\u0441\u0442\u044c \u0436\u0435 \u0442\u0435\u043f\u0435\u0440\u044c \u0436\u0438\u0442\u044c \u0441 \u043d\u0438\u043c\u0438, \u0438\u043c\u0435\u044f \u043e\u0434\u0438\u043d <code>move_only_function<\/code>, \u0433\u0434\u0435 \u043e\u043d\u0438 \u0438\u0441\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u044b, \u0438 \u043d\u0435 \u0438\u043c\u0435\u044f \u043a\u043e\u043f\u0438\u0440\u0443\u0435\u043c\u044b\u0439 \u0430\u043d\u0430\u043b\u043e\u0433 <code>move_only_function<\/code>?<\/p>\n<p>\u041a\u043e\u043c\u0438\u0442\u0435\u0442 \u043f\u043e\u0434\u0443\u043c\u0430\u043b \u0438 \u0440\u0435\u0448\u0438\u043b: \u00ab\u0412 \u044d\u0442\u0438\u0445 \u0441\u043b\u043e\u0432\u0430\u0445 \u0435\u0441\u0442\u044c \u0441\u043c\u044b\u0441\u043b, \u043d\u043e \u043c\u044b \u043d\u0435 \u0433\u043e\u0442\u043e\u0432\u044b \u0438\u0441\u043f\u0440\u0430\u0432\u0438\u0442\u044c \u0441\u0430\u043c <code>function<\/code>. \u041c\u044b \u043d\u0435 \u0433\u043e\u0442\u043e\u0432\u044b \u043d\u0430\u0440\u0443\u0448\u0438\u0442\u044c \u0443\u0441\u0442\u043e\u0438 \u0438 \u0442\u0440\u0430\u0434\u0438\u0446\u0438\u0438, \u043d\u0430 \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0441 2011-\u0433\u043e \u0433\u043e\u0434\u0430 \u043f\u043e\u043b\u0430\u0433\u0430\u044e\u0442\u0441\u044f \u0442\u044b\u0441\u044f\u0447\u0438 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0441\u0442\u043e\u0432\u00bb.<\/p>\n<p>\u0412\u043d\u0438\u043c\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u0447\u0438\u0442\u0430\u0442\u0435\u043b\u044c \u043a\u0438\u0432\u043d\u0443\u043b, \u0443\u0434\u0430\u043b\u044f\u0441\u044c \u0434\u043b\u044f \u0434\u0443\u043c \u043f\u0440\u043e\u0447\u044c. \u041d\u043e \u0432\u0441\u043a\u043e\u0440\u0435 \u0432\u0435\u0440\u043d\u0443\u043b\u0441\u044f \u0441 \u043f\u0440\u043e\u043f\u043e\u0437\u0430\u043b\u043e\u043c, \u043f\u0440\u0435\u0434\u043b\u0430\u0433\u0430\u0432\u0448\u0438\u043c \u0432\u0432\u0435\u0441\u0442\u0438 \u043d\u043e\u0432\u044b\u0439 \u043a\u043b\u0430\u0441\u0441, \u0432\u0430\u0440\u0438\u0430\u043d\u0442 <code>move_only_function<\/code>, \u043d\u043e \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u044e\u0449\u0438\u0439 \u043a\u043e\u043f\u0438\u0440\u0443\u0435\u043c\u044b\u0435 \u0442\u0438\u043f\u044b. \u0422\u0430\u043a \u0432 C++26 \u043f\u0440\u0438\u043d\u044f\u043b\u0438 <code>copyable_function<\/code> [<a href=\"https:\/\/wg21.link\/p2548\" rel=\"noopener noreferrer nofollow\">4<\/a>].<\/p>\n<h3>\u041f\u0435\u0440\u0432\u044b\u0439 \u043f\u0440\u0438\u043c\u0435\u0440<\/h3>\n<pre><code class=\"cpp\">auto lambda{[&amp;]() \/*const*\/ { \u2026 }};  copyable_function&lt;void(void)> func0{lambda}; const auto &amp; ref0{func0};  \/\/ \u0412\u0441\u0435 \u0445\u043e\u0440\u043e\u0448\u043e, \u043c\u044b \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u043c \/\/ \u043d\u0435\u043a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u043d\u0443\u044e (\u043f\u043e \u043f\u0435\u0440\u0435\u0434\u0430\u043d\u043d\u043e\u0439 \u0441\u0438\u0433\u043d\u0430\u0442\u0443\u0440\u0435) \u0444\u0443\u043d\u043a\u0446\u0438\u044e \/\/ \u0447\u0435\u0440\u0435\u0437 \u043d\u0435\u043a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u043d\u044b\u0439 \u043e\u0431\u044a\u0435\u043a\u0442 func0(); \/\/ Compilation error: \u043f\u044b\u0442\u0430\u0435\u043c\u0441\u044f \u0432\u044b\u0437\u0432\u0430\u0442\u044c \/\/ \u043d\u0435\u043a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u043d\u0443\u044e (\u043f\u043e \u043f\u0435\u0440\u0435\u0434\u0430\u043d\u043d\u043e\u0439 \u0441\u0438\u0433\u043d\u0430\u0442\u0443\u0440\u0435) \u0444\u0443\u043d\u043a\u0446\u0438\u044e \/\/ \u0447\u0435\u0440\u0435\u0437 \u043a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u043d\u044b\u0439 \u043e\u0431\u044a\u0435\u043a\u0442 ref0(); \/\/ operator() is NOT const!  \/\/ ---  copyable_function&lt;void(void) const> func1{lambda}; const auto &amp; ref1{func1};  \/\/ All is OK. \u0412\u044b\u0437\u044b\u0432\u0430\u0435\u043c \u043a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u043d\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e \/\/ 1) \u0427\u0435\u0440\u0435\u0437 \u043d\u0435\u043a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u043d\u044b\u0439 \u043e\u0431\u044a\u0435\u043a\u0442 func1(); \/\/ 2) \u0427\u0435\u0440\u0435\u0437 \u043a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u043d\u044b\u0439 \u043e\u0431\u044a\u0435\u043a\u0442 ref1(); \/\/ operator() is const!<\/code><\/pre>\n<h3>\u0412\u0442\u043e\u0440\u043e\u0439 \u043f\u0440\u0438\u043c\u0435\u0440<\/h3>\n<pre><code class=\"cpp\">auto lambda{[&amp;]() mutable { \u2026 }};  copyable_function&lt;void(void)> func{lambda}; const auto &amp; ref{func};  \/\/ \u0412\u0441\u0435 \u0445\u043e\u0440\u043e\u0448\u043e: \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u043c \/\/ \u043d\u0435\u043a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u043d\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0447\u0435\u0440\u0435\u0437 \u043d\u0435\u043a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u043d\u044b\u0439 \u043e\u0431\u044a\u0435\u043a\u0442 func(); \/\/ Compilation error: \u043f\u044b\u0442\u0430\u0435\u043c\u0441\u044f \u0432\u044b\u0437\u0432\u0430\u0442\u044c \/\/ \u043d\u0435\u043a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u043d\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0447\u0435\u0440\u0435\u0437 \u043a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u043d\u044b\u0439 \u043e\u0431\u044a\u0435\u043a\u0442 ref();  \/\/ Compilation error: \u043d\u0435 \u043c\u043e\u0436\u0435\u043c \/\/ \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u043d\u0435\u043a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u043d\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u043a\u0430\u043a \u043a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u043d\u0443\u044e copyable_function&lt;void(void) const> tmp{lambda};<\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c-\u0442\u043e \u0442\u043e\u0447\u043d\u043e \u0432\u0441\u0435 \u0434\u043e\u043b\u0436\u043d\u044b \u0431\u044b\u0442\u044c \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u044b? \ud83d\ude42<\/p>\n<p>\u041d\u0435 \u0441\u043a\u0430\u0436\u0438\u0442\u0435! \u0427\u0442\u043e \u043c\u044b \u0443\u043f\u0443\u0441\u0442\u0438\u043b\u0438 \u2014 \u0438 <code>function<\/code>, \u0438 <code>move_only_function<\/code>, \u0438 <code>copyable_function<\/code> \u2014 \u0432\u0441\u0435 \u043e\u043d\u0438 \u043f\u043e \u0441\u0432\u043e\u0435\u0439 \u0441\u0435\u043c\u0430\u043d\u0442\u0438\u043a\u0435 <strong>\u0432\u043b\u0430\u0434\u0435\u044e\u0442<\/strong> \u0437\u0430\u0432\u0435\u0440\u043d\u0443\u0442\u044b\u043c\u0438 \u0432 \u043d\u0438\u0445 \u0444\u0443\u043d\u043a\u0446\u0438\u044f\u043c\u0438. \u0410 \u0447\u0442\u043e \u0435\u0441\u043b\u0438, \u0435\u0441\u043b\u0438 \u0432\u0441\u0435, \u0447\u0442\u043e \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u2014 \u044d\u0442\u043e <strong>\u043d\u0435\u0432\u043b\u0430\u0434\u0435\u044e\u0449\u0438\u0439<\/strong> reference \u043d\u0430 callable \u043e\u0431\u044a\u0435\u043a\u0442? \u041a\u043e\u0442\u043e\u0440\u044b\u0439, \u043c\u0435\u0436\u0434\u0443 \u043f\u0440\u043e\u0447\u0438\u043c, \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0434\u0430\u0436\u0435 \u043d\u0438 \u043a\u043e\u043f\u0438\u0440\u0443\u0435\u043c\u044b\u043c, \u043d\u0438 \u043f\u0435\u0440\u0435\u043c\u0435\u0449\u0430\u0435\u043c\u044b\u043c.<\/p>\n<h2>Non-owning reference to a Callable<\/h2>\n<p>\u0412\u0441\u0442\u0440\u0435\u0447\u0430\u0439\u0442\u0435 <code>function_ref<\/code>, \u043f\u0440\u0438\u043d\u044f\u0442\u044b\u0439 \u0432 C++26 [<a href=\"https:\/\/wg21.link\/P0792\" rel=\"noopener noreferrer nofollow\">5<\/a>]!<\/p>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c \u0442\u043e, \u0447\u0442\u043e \u043d\u0435\u043b\u044c\u0437\u044f \u043d\u0438 \u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c, \u043d\u0438 \u043f\u0435\u0440\u0435\u043c\u0435\u0449\u0430\u0442\u044c:<\/p>\n<pre><code class=\"cpp\">#include &lt;functional>  struct A {     A() { }     A(const A&amp;) = delete;     A(A&amp;&amp;) = delete;     void operator()() { } };  int main() {     A obj;      \/\/ Compile errors:     std::function&lt;void(void)> func1 = obj;     std::move_only_function&lt;void(void)> func2 = std::move(obj);     std::copyable_function&lt;void(void)> func3 = obj;      \/\/ All is OK:     std::function_ref&lt;void(void)> func4 = obj;     func4(); }<\/code><\/pre>\n<p>\u0418, \u0432\u0441\u043f\u043e\u043c\u0438\u043d\u0430\u044f \u043e \u043a\u043e\u043b\u0431\u044d\u043a\u0430\u0445, \u043f\u043e\u0441\u043b\u0443\u0436\u0438\u0432\u0448\u0438\u0445 \u043e\u0434\u043d\u0438\u043c \u0438\u0437 \u0432\u0435\u0434\u0443\u0449\u0438\u0445 \u044e\u0437\u043a\u0435\u0439\u0441\u043e\u0432, \u0434\u0435\u043c\u043e\u043d\u0441\u0442\u0440\u0438\u0440\u0443\u044e\u0449\u0438\u0445 \u043f\u043e\u043b\u0435\u0437\u043d\u043e\u0441\u0442\u044c <code>function<\/code>. \u0410 \u0440\u0430\u0437\u0432\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u044f, \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u044e\u0449\u0430\u044f \u043a\u043e\u043b\u0431\u044d\u043a, \u0436\u0435\u043b\u0430\u0435\u0442 \u0432\u043b\u0430\u0434\u0435\u0442\u044c \u0438\u043c? \u0416\u0435\u043b\u0430\u0435\u0442 \u043e\u0432\u0435\u0440\u0445\u0435\u0434 \u0432 \u0432\u0438\u0434\u0435 \u0430\u043b\u043b\u043e\u043a\u0430\u0446\u0438\u0439? \u0412 \u0431\u043e\u043b\u044c\u0448\u0438\u043d\u0441\u0442\u0432\u0435 \u0441\u043b\u0443\u0447\u0430\u0435\u0432 \u2014 \u043d\u0435\u0442. <\/p>\n<p>\u0422\u0430\u043a \u0447\u0442\u043e, \u043d\u0430 \u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435, \u044d\u0442\u043e \u0431\u043e\u043b\u044c\u0448\u0435 \u044e\u0437\u043a\u0435\u0439\u0441 \u0434\u043b\u044f <code>function_ref<\/code>:<\/p>\n<pre><code class=\"cpp\">data retry(size_t times, function_ref&lt;data()> action) { \/\/ ... }<\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0442\u043e \u043d\u0430\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u044b\u0445 \u043e\u0431\u0435\u0440\u0442\u043e\u043a \u0445\u0432\u0430\u0442\u0438\u0442? <strong>\u0418\u043b\u0438, \u043c\u043e\u0436\u0435\u0442, \u0441\u0430\u043c\u043e\u0435 \u0432\u0440\u0435\u043c\u044f \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043f\u0440\u043e\u043f\u043e\u0437\u0430\u043b, \u043f\u0440\u0435\u0434\u043b\u0430\u0433\u0430\u044e\u0449\u0438\u0439 \u043a\u0430\u043a\u0443\u044e-\u043d\u0438\u0431\u0443\u0434\u044c \u0435\u0449\u0435?<\/strong> \ud83d\ude42<\/p>\n<blockquote>\n<p>\u041e\u043f\u0443\u0431\u043b\u0438\u043a\u043e\u0432\u0430\u043d\u043e \u043f\u0440\u0438 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0435 <a href=\"https:\/\/t.me\/cppmoscow_info\" rel=\"noopener noreferrer nofollow\">C++ Moscow<\/a><\/p>\n<\/blockquote>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \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\/articles\/788524\/\"> https:\/\/habr.com\/ru\/articles\/788524\/<\/a><br \/><\/br><\/br><\/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-366156","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/366156","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=366156"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/366156\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=366156"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=366156"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=366156"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}