{"id":346826,"date":"2023-03-16T15:00:17","date_gmt":"2023-03-16T15:00:17","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=346826"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=346826","title":{"rendered":"<span>\u041a\u0440\u0430\u0442\u043a\u0438\u0439 \u043e\u0431\u0437\u043e\u0440 \u043d\u043e\u0432\u043e\u0432\u0432\u0435\u0434\u0435\u043d\u0438\u0439 C++23: deducing this<\/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<p>\u0414\u043e\u043a\u0443\u043c\u0435\u043d\u0442 <em>\u00abdeducing this\u00bb<\/em>, \u043f\u0440\u0438\u043d\u044f\u0442\u044b\u0439 \u0432 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0439 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442 C++, \u0432\u0432\u043e\u0434\u0438\u0442 \u043d\u043e\u0432\u044b\u0439, \u0442\u0440\u0435\u0442\u0438\u0439 \u0442\u0438\u043f \u043c\u0435\u0442\u043e\u0434\u043e\u0432 \u043a\u043b\u0430\u0441\u0441\u043e\u0432, \u0441\u043e\u0447\u0435\u0442\u0430\u044e\u0449\u0438\u0439 \u0432 \u0441\u0435\u0431\u0435 \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u0430 \u0434\u0432\u0443\u0445 \u0443\u0436\u0435 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0445: <em>\u043d\u0435\u0441\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0445<\/em> \u0438 <em>\u0441\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0445<\/em>, \u043e\u0442\u043a\u0440\u044b\u0432\u0430\u044e\u0449\u0438\u0439 \u043f\u0435\u0440\u0435\u0434 \u043d\u0430\u043c\u0438 \u043d\u043e\u0432\u044b\u0435 \u0433\u043e\u0440\u0438\u0437\u043e\u043d\u0442\u044b:<\/p>\n<ol>\n<li>\n<p>\u0414\u0435\u0434\u0443\u043f\u043b\u0438\u043a\u0430\u0446\u0438\u044f \u0431\u043e\u043b\u044c\u0448\u043e\u0433\u043e \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0430 \u043a\u043e\u0434\u0430<\/p>\n<\/li>\n<li>\n<p>\u0412\u044b\u0442\u0435\u0441\u043d\u0435\u043d\u0438\u0435 CRTP (<em>Curiously Recuring Template Pattern<\/em>) \u043d\u0430 \u0441\u0432\u0430\u043b\u043a\u0443 \u0438\u0441\u0442\u043e\u0440\u0438\u0438, \u0435\u0433\u043e \u0437\u0430\u043c\u0435\u043d\u0430 \u0431\u043e\u043b\u0435\u0435 \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u0438 \u043e\u0447\u0435\u0432\u0438\u0434\u043d\u043e \u043f\u043e\u043d\u044f\u0442\u043d\u043e\u0439 \u0437\u0430\u043f\u0438\u0441\u044c\u044e<\/p>\n<\/li>\n<li>\n<p>\u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u044b\u0435 \u043b\u044f\u043c\u0431\u0434\u044b<\/p>\n<\/li>\n<\/ol>\n<p>\u0418 \u0434\u0440\u0443\u0433\u043e\u0435.<\/p>\n<p>\u041d\u043e \u043f\u0440\u0435\u0436\u0434\u0435 \u0447\u0435\u043c \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u0441\u0430\u043c\u043e \u043d\u043e\u0432\u043e\u0432\u0432\u0435\u0434\u0435\u043d\u0438\u0435 \u0438 \u0435\u0433\u043e \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0435 \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u044f, \u0443\u0433\u043b\u0443\u0431\u0438\u043c\u0441\u044f \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u0432 \u0438\u0441\u0442\u043e\u0440\u0438\u044e \u0438 \u043f\u043e\u043f\u044b\u0442\u0430\u0435\u043c\u0441\u044f \u043f\u043e\u043d\u044f\u0442\u044c, \u043f\u043e\u0447\u0435\u043c\u0443 \u0432 \u043d\u0435\u043c \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u0432\u043e\u0437\u043d\u0438\u043a\u043b\u0430 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u044c.<\/p>\n<h2>\u041c\u043e\u0442\u0438\u0432\u0430\u0446\u0438\u044f<\/h2>\n<p>\u041d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 C++03, \u043c\u0435\u0442\u043e\u0434\u044b \u043c\u043e\u0433\u0443\u0442 \u0438\u043c\u0435\u0442\u044c <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/language\/cv\" rel=\"noopener noreferrer nofollow\"><strong><em>cv-\u043a\u0432\u0430\u043b\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440\u044b<\/em><\/strong><\/a>, \u0442\u0430\u043a \u0447\u0442\u043e \u0441\u0442\u0430\u043b\u0438 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u0438, \u043a\u043e\u0433\u0434\u0430 \u0435\u0441\u0442\u044c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u044c \u043a\u0430\u043a \u0432 <code>const<\/code>, \u0442\u0430\u043a \u0438 \u043d\u0435-<code>const<\/code> \u043f\u0435\u0440\u0435\u0433\u0440\u0443\u0437\u043a\u0435 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u043e\u0433\u043e \u043c\u0435\u0442\u043e\u0434\u0430 (\u0434\u043b\u044f \u043a\u0440\u0430\u0442\u043a\u043e\u0441\u0442\u0438 \u0432\u043e\u0437\u0434\u0435\u0440\u0436\u0438\u043c\u0441\u044f \u043e\u0442 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u043d\u0438\u044f <code>volatile<\/code> \u043f\u0435\u0440\u0435\u0433\u0440\u0443\u0437\u043e\u043a).<\/p>\n<p>\u0412\u043e \u043c\u043d\u043e\u0433\u0438\u0445 \u0441\u043b\u0443\u0447\u0430\u044f\u0445 \u043c\u0435\u0436\u0434\u0443 \u0438\u0445 \u043b\u043e\u0433\u0438\u043a\u043e\u0439 \u043d\u0435\u0442 \u043d\u0438\u043a\u0430\u043a\u043e\u0439 \u0440\u0430\u0437\u043d\u0438\u0446\u044b \u2014 \u043e\u0442\u043b\u0438\u0447\u0430\u044e\u0442\u0441\u044f \u043b\u0438\u0448\u044c <em>\u043a\u0432\u0430\u043b\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u044b\u0445 \u0442\u0438\u043f\u043e\u0432<\/em>, \u0442\u0430\u043a \u0447\u0442\u043e \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0438\u043b\u0438 \u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435, \u043f\u043e\u0434\u0433\u043e\u043d\u044f\u044f \u043a\u0432\u0430\u043b\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440\u044b, \u0438\u043b\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0442\u0430\u043a\u0438\u0435 \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c\u044b \u043a\u0430\u043a <code>const_cast<\/code>:<\/p>\n<pre><code class=\"cpp\">class TextBlock { public:   char const&amp; operator[](size_t position) const {     \/\/ ...     return text[position];   }    char&amp; operator[](size_t position) {     return const_cast&lt;char&amp;>(       static_cast&lt;TextBlock const&amp;>(*this)[position]     );   }   \/\/ ... };<\/code><\/pre>\n<p>\u041d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 C++11, \u043c\u0435\u0442\u043e\u0434\u044b \u043c\u043e\u0433\u0443\u0442 \u0438\u043c\u0435\u0442\u044c \u0442\u0430\u043a\u0436\u0435 \u0438 <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/language\/member_functions#member_functions_with_ref-qualifier\" rel=\"noopener noreferrer nofollow\"><strong><em>ref-\u043a\u0432\u0430\u043b\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440\u044b<\/em><\/strong><\/a>, \u0442\u0430\u043a \u0447\u0442\u043e \u0442\u0435\u043f\u0435\u0440\u044c \u0432\u043c\u0435\u0441\u0442\u043e \u0434\u0432\u0443\u0445 \u043f\u0435\u0440\u0435\u0433\u0440\u0443\u0437\u043e\u043a \u043e\u0434\u043d\u043e\u0433\u043e \u043c\u0435\u0442\u043e\u0434\u0430 \u043d\u0430\u043c \u043c\u043e\u0433\u0443\u0442 \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u0442\u044c\u0441\u044f \u0447\u0435\u0442\u044b\u0440\u0435: <code>&amp;<\/code>, <code>const&amp;<\/code>, <code>&amp;&amp;<\/code>, <code>const&amp;&amp;<\/code>, \u0438 \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u0442\u0440\u0438 \u0441\u043f\u043e\u0441\u043e\u0431\u0430 \u0440\u0435\u0448\u0438\u0442\u044c \u0434\u0430\u043d\u043d\u0443\u044e \u0437\u0430\u0434\u0430\u0447\u0443 (<a href=\"https:\/\/godbolt.org\/z\/Pqvn3rKvh\" rel=\"noopener noreferrer nofollow\">\u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043a\u043e\u0434, \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d\u043d\u044b\u0439 \u043d\u0438\u0436\u0435<\/a>):<\/p>\n<ol>\n<li>\n<p>\u041f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e \u043e\u0434\u043d\u043e\u0433\u043e \u043c\u0435\u0442\u043e\u0434\u0430 \u0447\u0435\u0442\u044b\u0440\u0435\u0436\u0434\u044b<\/p>\n<\/li>\n<li>\n<p>\u0414\u0435\u043b\u0435\u0433\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0442\u0440\u0438 \u043f\u0435\u0440\u0435\u0433\u0440\u0443\u0437\u043a\u0438 \u0447\u0435\u0442\u0432\u0435\u0440\u0442\u043e\u0439, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f <code>static_cast<\/code> \u0438 <code>const_cast<\/code><\/p>\n<\/li>\n<li>\n<p>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u0443\u044e \u0448\u0430\u0431\u043b\u043e\u043d\u043d\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e<\/p>\n<\/li>\n<\/ol>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/56c\/c55\/2f1\/56cc552f1961aa28432f6f2f1380b0b6.png\" alt=\"\" title=\"\" width=\"960\" height=\"595\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/56c\/c55\/2f1\/56cc552f1961aa28432f6f2f1380b0b6.png\"\/><\/figure>\n<p>\u041d\u043e \u043d\u0438 \u043e\u0434\u0438\u043d \u0438\u0437 \u044d\u0442\u0438\u0445 \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u0432 \u043d\u0435 \u0438\u0437\u0431\u0430\u0432\u043b\u044f\u0435\u0442 \u043d\u0430\u0441 \u043e\u0442 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u0438 \u0447\u0435\u0442\u044b\u0440\u0435\u0436\u0434\u044b \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0442\u044c \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u043e\u0434\u0438\u043d \u0438 \u0442\u043e\u0442 \u0436\u0435 \u043c\u0435\u0442\u043e\u0434.<\/p>\n<p>\u0415\u0441\u043b\u0438 \u0431\u044b \u043c\u043e\u0433\u043b\u0438 \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0447\u0442\u043e-\u0442\u043e \u0432\u0440\u043e\u0434\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043d\u0438\u0436\u0435, \u043d\u043e \u0432\u0435\u0434\u0443\u0449\u0435\u0439 \u0441\u0435\u0431\u044f \u043a\u0430\u043a \u0447\u043b\u0435\u043d \u043a\u043b\u0430\u0441\u0441\u0430, \u044d\u0442\u043e \u0440\u0435\u0448\u0438\u043b\u043e \u0431\u044b \u0432\u0441\u0435 \u043d\u0430\u0448\u0438 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b:<\/p>\n<pre><code class=\"cpp\">template &lt;typename T> class optional {     \/\/ ...     template &lt;typename Opt>     friend decltype(auto) value(Opt&amp;&amp; o) {         if (o.has_value()) {             return forward&lt;Opt>(o).m_value;         }         throw bad_optional_access();     }     \/\/ ... };<\/code><\/pre>\n<p>\u041d\u043e \u043c\u044b \u043d\u0435 \u043c\u043e\u0436\u0435\u043c. \u0422\u043e\u0447\u043d\u0435\u0439, <em>\u043d\u0435 \u043c\u043e\u0433\u043b\u0438<\/em>. \u0414\u043e C++23<\/p>\n<h2>\u041c\u0435\u0447\u0442\u044b \u0432\u043e\u043f\u043b\u043e\u0449\u0430\u044e\u0442\u0441\u044f \u0432 \u0440\u0435\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u044c<\/h2>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0436\u0435 \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0442\u044c \u043c\u0435\u0442\u043e\u0434\u044b, \u044f\u0432\u043d\u043e \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u044e\u0449\u0438\u0435 \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0430 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u0430 \u043e\u0431\u044a\u0435\u043a\u0442, \u043d\u0430\u0434 \u043a\u043e\u0442\u043e\u0440\u044b\u043c \u043e\u043d\u0438 \u0431\u044b\u043b\u0438 \u0432\u044b\u0437\u0432\u0430\u043d\u044b:<\/p>\n<pre><code class=\"cpp\">struct X {     template &lt;typename Self>     void foo(this Self&amp;&amp;) { } };  void example(X&amp; x) {     x.foo();            \/\/ Self = X&amp;     move(x).foo();      \/\/ Self = X     X{}.foo();          \/\/ Self = X }<\/code><\/pre>\n<p>\u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, \u0442\u0435\u043f\u0435\u0440\u044c \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043f\u0435\u0440\u0435\u043f\u0438\u0441\u0430\u0442\u044c \u0432\u0441\u044e \u0442\u0443 \u043f\u0440\u043e\u0441\u0442\u044b\u043d\u044e \u043a\u043e\u0434\u0430, \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u044e\u0449\u0443\u044e \u043c\u0435\u0442\u043e\u0434 <code>value<\/code> \u0443 <code>optional<\/code>, \u0433\u043e\u0440\u0430\u0437\u0434\u043e \u0431\u043e\u043b\u0435\u0435 \u043a\u043e\u043c\u043f\u0430\u043a\u0442\u043d\u043e, \u043c\u0435\u043d\u044c\u0448\u0435 \u0447\u0435\u043c \u0432 \u0434\u0435\u0441\u044f\u0442\u043e\u043a \u0441\u0442\u0440\u043e\u043a:<\/p>\n<pre><code class=\"cpp\">template &lt;typename T> struct optional {   template &lt;typename Self>   constexpr auto&amp;&amp; value(this Self&amp;&amp; self) {     if (!self.has_value()) {       throw bad_optional_access();     }     return forward&lt;Self>(self).m_value; }<\/code><\/pre>\n<p>\u041c\u044b \u043f\u043e\u0434\u0447\u0438\u043d\u0438\u043b\u0438 \u0441\u0432\u043e\u0438\u043c \u0446\u0435\u043b\u044f\u043c \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c \u0432\u044b\u0432\u043e\u0434\u0430 \u0442\u0438\u043f\u043e\u0432 \u0432\u043e \u0432\u0441\u0435\u0439 \u0441\u0432\u043e\u0435\u0439 \u043c\u043e\u0449\u0438! \u0418 \u0432\u0430\u0436\u043d\u043e \u043e\u0442\u043c\u0435\u0442\u0438\u0442\u044c, \u0447\u0442\u043e \u044d\u0442\u043e <em>\u0432\u0441\u0435 \u0442\u043e\u0442 \u0436\u0435 \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c<\/em>, \u043f\u0440\u043e\u044f\u0432\u043b\u044f\u044e\u0449\u0438\u0439 \u0441\u0435\u0431\u044f \u0432 \u043e\u0431\u044b\u0447\u043d\u044b\u0445 \u0448\u0430\u0431\u043b\u043e\u043d\u043d\u044b\u0445 \u0444\u0443\u043d\u043a\u0446\u0438\u044f\u0445 \u0438 \u043c\u0435\u0442\u043e\u0434\u0430\u0445.<\/p>\n<p><em>Deducing this<\/em> \u043d\u0435 \u0432\u043d\u043e\u0441\u0438\u0442 \u043d\u0438\u043a\u0430\u043a\u0438\u0445 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0439 \u0432 \u043f\u0440\u0430\u0432\u0438\u043b\u0430 \u0432\u044b\u0432\u043e\u0434\u0430 \u0442\u0438\u043f\u043e\u0432, \u043e\u043d \u043b\u0438\u0448\u044c \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u044f\u0432\u043d\u043e\u0435 \u043e\u0431\u044a\u044f\u0432\u043b\u0435\u043d\u0438\u0435 <em>\u043e\u0431\u044a\u0435\u043a\u0442\u043d\u043e\u0433\u043e \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430<\/em> (<em>explicit object parameter<\/em>) \u0432 \u0441\u043f\u0438\u0441\u043a\u0435 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u0432 \u043c\u0435\u0442\u043e\u0434\u043e\u0432. \u041f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0434\u043e \u044d\u0442\u043e\u0433\u043e \u0432 \u043c\u0435\u0442\u043e\u0434\u0430\u0445 \u043f\u0440\u0438\u0441\u0443\u0442\u0441\u0442\u0432\u043e\u0432\u0430\u043b \u043b\u0438\u0448\u044c <em>\u043d\u0435\u044f\u0432\u043d\u043e<\/em> (<em>implicit object parameter<\/em>), \u0432 \u0432\u0438\u0434\u0435 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f <code>this<\/code>.<\/p>\n<p>\u0412\u0430\u0436\u043d\u043e \u043e\u0442\u043c\u0435\u0442\u0438\u0442\u044c, \u0447\u0442\u043e \u0432\u044b\u0432\u043e\u0434 \u0442\u0438\u043f\u043e\u0432 \u0441\u043f\u043e\u0441\u043e\u0431\u0435\u043d \u0432\u044b\u0432\u043e\u0434\u0438\u0442\u044c \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u043d\u044b\u0435 \u0442\u0438\u043f\u044b:<\/p>\n<pre><code class=\"cpp\">struct X {   template &lt;typename Self>   void foo(this Self&amp;&amp;, int); };  struct D : X { };  void example(X&amp; x, D&amp; d) {     x.foo(1);               \/\/ Self=X&amp;     move(x).foo(2);         \/\/ Self=X     d.foo(3);               \/\/ Self=D&amp; }<\/code><\/pre>\n<p>\u041d\u0430 \u043c\u0435\u0442\u043e\u0434\u044b, \u043e\u0431\u044a\u044f\u0432\u043b\u0435\u043d\u043d\u044b\u0435 \u0441 \u044f\u0432\u043d\u044b\u043c \u043e\u0431\u044a\u0435\u043a\u0442\u043d\u044b\u043c \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u043c, \u0442\u0430\u043a\u0436\u0435 \u043d\u0430\u043a\u043b\u0430\u0434\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0440\u044f\u0434 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0439:<\/p>\n<ol>\n<li>\n<p><strong>\u041e\u043d\u0438 \u043d\u0435 \u043c\u043e\u0433\u0443\u0442 \u0431\u044b\u0442\u044c \u043e\u0431\u044a\u044f\u0432\u043b\u0435\u043d\u044b \u0441\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u043c\u0438<\/strong><\/p>\n<\/li>\n<\/ol>\n<p>\u041f\u0440\u0438\u0447\u0438\u043d\u0430 \u044d\u0442\u043e\u0433\u043e \u0437\u0430\u043a\u043b\u044e\u0447\u0430\u0435\u0442\u0441\u044f \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e \u0445\u043e\u0442\u044c \u0438 \u0432\u043d\u0435\u0448\u043d\u0435 \u044d\u0442\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0432\u044b\u0433\u043b\u044f\u0434\u044f\u0442 \u0438 \u0432\u0435\u0434\u0443\u0442 \u0441\u0435\u0431\u044f \u043a\u0430\u043a \u043e\u0431\u044b\u0447\u043d\u044b\u0435 \u043d\u0435\u0441\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0435 \u043c\u0435\u0442\u043e\u0434\u044b, \u043d\u043e \u0432\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0435 \u043e\u043d\u0438 \u0432\u0435\u0434\u0443\u0442 \u0441\u0435\u0431\u044f \u0432 \u0442\u043e\u0447\u043d\u043e\u0441\u0442\u0438 \u043a\u0430\u043a \u0441\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0435: \u0432 \u043d\u0438\u0445 <strong>\u043d\u0435\u0434\u043e\u0441\u0442\u0443\u043f\u0435\u043d \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c <\/strong><code>this<\/code>, \u0438 \u0435\u0434\u0438\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0439 \u0441\u043f\u043e\u0441\u043e\u0431 \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u043e\u0432\u0430\u0442\u044c \u0432 \u043d\u0438\u0445 \u0441 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u043c \u043a\u043b\u0430\u0441\u0441\u0430 \u2014 \u0447\u0435\u0440\u0435\u0437 \u044f\u0432\u043d\u044b\u0439 \u043e\u0431\u044a\u0435\u043a\u0442\u043d\u044b\u0439 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440.<\/p>\n<p>\u041a\u0440\u043e\u043c\u0435 \u0442\u043e\u0433\u043e, <strong>\u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u043d\u0430 \u0442\u0430\u043a\u0438\u0435 \u043c\u0435\u0442\u043e\u0434\u044b \u2014 \u044d\u0442\u043e \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u043d\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u044e<\/strong>, \u0430 \u043d\u0435 \u0447\u043b\u0435\u043d \u043a\u043b\u0430\u0441\u0441\u0430.<\/p>\n<ol start=\"2\">\n<li>\n<p><strong>\u041e\u043d\u0438 \u043d\u0435 \u043c\u043e\u0433\u0443\u0442 \u0431\u044b\u0442\u044c \u043e\u0431\u044a\u044f\u0432\u043b\u0435\u043d\u044b \u0432\u0438\u0440\u0442\u0443\u0430\u043b\u044c\u043d\u044b\u043c\u0438<\/strong><\/p>\n<\/li>\n<li>\n<p><strong>\u041e\u043d\u0438 \u043d\u0435 \u043c\u043e\u0433\u0443\u0442 \u0431\u044b\u0442\u044c \u043e\u0431\u044a\u044f\u0432\u043b\u0435\u043d\u044b \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c <em>ref \u0438\u043b\u0438 cv \u043a\u0432\u0430\u043b\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440\u043e\u0432<\/em><\/strong><\/p>\n<\/li>\n<\/ol>\n<p>\u0422\u0430\u043a \u043a\u0430\u043a \u043e\u0431\u044a\u044f\u0432\u043b\u0435\u043d\u0438\u0435 \u044f\u0432\u043d\u043e\u0433\u043e \u043e\u0431\u044a\u0435\u043a\u0442\u043d\u043e\u0433\u043e \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430 \u0443\u0436\u0435 \u043d\u0435\u0441\u0435\u0442 \u0432 \u0441\u0435\u0431\u0435 \u0432\u0441\u044e \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u0443\u044e \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u043e \u0435\u0433\u043e \u0442\u0438\u043f\u0435:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/c45\/48f\/0c1\/c4548f0c1b8117ca8273c4b71f9f4eea.png\" alt=\"\" title=\"\" width=\"798\" height=\"223\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/c45\/48f\/0c1\/c4548f0c1b8117ca8273c4b71f9f4eea.png\"\/><\/figure>\n<h2>\u041d\u043e \u0435\u0441\u0442\u044c \u043d\u044e\u0430\u043d\u0441\u044b<\/h2>\n<p>\u041e\u0434\u043d\u0430\u043a\u043e \u0441 \u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u0441\u0438\u043b\u043e\u0439 \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442 \u0438 \u0431\u043e\u043b\u044c\u0448\u0430\u044f \u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0441\u0442\u044c. \u0422\u0430\u043a, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u043d\u043e\u0432\u044b\u0439 \u0442\u0438\u043f \u043c\u0435\u0442\u043e\u0434\u043e\u0432, \u043c\u044b \u0434\u043e\u043b\u0436\u043d\u044b \u043f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u043e \u043f\u043e\u043c\u043d\u0438\u0442\u044c \u043e \u0442\u043e\u043c, \u043d\u0430\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043c\u043e\u0433\u0443\u0449\u0435\u0441\u0442\u0432\u0435\u043d \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c \u0432\u044b\u0432\u043e\u0434\u0430 \u0442\u0438\u043f\u043e\u0432:<\/p>\n<pre><code class=\"cpp\">struct B {     int i = 0;      template &lt;typename Self> auto&amp;&amp; f1(this Self&amp;&amp; self) { return forward&lt;Self>(self).i; } };  struct D: B {     double i = 3.14; };<\/code><\/pre>\n<p>\u0422\u043e\u0433\u0434\u0430 \u043a\u0430\u043a <code>B().f1()<\/code> \u0432 \u043a\u043e\u0434\u0435 \u0432\u044b\u0448\u0435 \u0432\u0435\u0440\u043d\u0435\u0442 \u0441\u0441\u044b\u043b\u043a\u0443 \u043d\u0430 <code>B::i<\/code>, <code>D().f5()<\/code> \u0432\u0435\u0440\u043d\u0435\u0442 \u0441\u0441\u044b\u043b\u043a\u0443 \u043d\u0430 <code>D::i<\/code>, \u0442\u0430\u043a \u043a\u0430\u043a <code>self<\/code> \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0441\u0441\u044b\u043b\u043a\u043e\u0439 \u043d\u0430 <code>D<\/code>.<\/p>\n<p>\u0415\u0441\u043b\u0438 \u0436\u0435 \u043c\u044b \u0445\u043e\u0442\u0438\u043c \u043f\u043e\u043b\u0443\u0447\u0430\u0442\u044c \u0441\u0441\u044b\u043b\u043a\u0443 \u043d\u0430 <code>B::i<\/code> \u0432\u0441\u0435\u0433\u0434\u0430, \u043d\u0430\u043c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u044f\u0432\u043d\u043e \u043f\u0440\u0435\u0434\u0443\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u044d\u0442\u043e \u0432 \u043a\u043e\u0434\u0435:<\/p>\n<pre><code class=\"cpp\">template &lt;typename Self> auto&amp;&amp; f1(this Self&amp;&amp; self) {     return forward&lt;Self>(self).B::i; }<\/code><\/pre>\n<p>\u0414\u0440\u0443\u0433\u043e\u0439 \u043e\u043f\u0430\u0441\u043d\u043e\u0441\u0442\u044c\u044e \u043d\u0430 \u043d\u0430\u0448\u0435\u043c \u043f\u0443\u0442\u0438 \u043c\u043e\u0436\u0435\u0442 \u0441\u043b\u0443\u0436\u0438\u0442\u044c \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 \u043f\u0440\u0438\u0432\u0430\u0442\u043d\u043e\u0433\u043e \u043d\u0430\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u043d\u0438\u044f. \u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u043a\u043e\u0434:<\/p>\n<pre><code class=\"cpp\">class B {     int i; public:     template &lt;typename Self>     auto&amp;&amp; get(this Self&amp;&amp; self) {         return forward&lt;Self>(self).B::i;     } };  class D: private B {     double i; public:     using B::get; };  D().get(); \/\/ error<\/code><\/pre>\n<p>\u041c\u044b, \u043a\u0430\u0437\u0430\u043b\u043e\u0441\u044c \u0431\u044b, \u043f\u0440\u0435\u0434\u043e\u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0441\u044c \u043a\u0430\u043a \u043c\u043e\u0433\u043b\u0438. \u041e\u0434\u043d\u0430\u043a\u043e \u043d\u0435\u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e. \u041c\u044b \u043d\u0435 \u043c\u043e\u0436\u0435\u043c \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0434\u043e\u0441\u0442\u0443\u043f \u043a <code>B::i<\/code> \u0438\u0437 <code>D<\/code>, \u0442\u0430\u043a \u043a\u0430\u043a \u043d\u0430\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u043d\u0438\u0435 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043f\u0440\u0438\u0432\u0430\u0442\u043d\u044b\u043c.<\/p>\n<p>\u041d\u043e \u0438 \u0434\u043b\u044f \u044d\u0442\u043e\u0439 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u0440\u0435\u0448\u0435\u043d\u0438\u0435:<\/p>\n<pre><code class=\"cpp\">class B {     int i; public:     template &lt;typename Self>     auto&amp;&amp; get(this Self&amp;&amp; self) {         \/\/ like_t \u2014 \u0444\u0443\u043d\u043a\u0446\u0438\u044f, \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u044e\u0449\u0430\u044f ref \u0438 cv \u043a\u0432\u0430\u043b\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440\u044b         \/\/ \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u043f\u0435\u0440\u0435\u0434\u0430\u043d\u043d\u043e\u0433\u043e \u0442\u0438\u043f\u0430 \u043a\u043e \u0432\u0442\u043e\u0440\u043e\u043c\u0443         \/\/ \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, like_t&lt;int&amp; double> = double&amp;         return ((like_t&lt;Self, B>&amp;&amp;)self).i;     } };  class D : private B {     double i; public:     using B::get; };  D().get(); \/\/ now ok, and returns B::i<\/code><\/pre>\n<p>\u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0432 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d\u0438\u0435 \u0432 \u0441\u0442\u0438\u043b\u0435 \u0441\u0438 \u0434\u043b\u044f \u0438\u0437\u0431\u0435\u0436\u0430\u043d\u0438\u044f \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u0434\u043e\u0441\u0442\u0443\u043f\u0430, \u043c\u044b \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043b\u0438 \u0436\u0435\u043b\u0430\u0435\u043c\u043e\u0435 \u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u0435.<\/p>\n<h2>\u041f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u0435<\/h2>\n<p>\u0418\u0442\u0430\u043a, \u043d\u0435\u043c\u043d\u043e\u0436\u043a\u043e \u043f\u043e\u0433\u043e\u0432\u043e\u0440\u0438\u0432 \u043e \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0438 \u043d\u043e\u0432\u043e\u0432\u0432\u0435\u0434\u0435\u043d\u0438\u044f \u0438 \u043e\u0431 \u043e\u043f\u0430\u0441\u043d\u043e\u0441\u0442\u044f\u0445, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u043e\u0433\u0443\u0442 \u043d\u0430\u0441 \u043f\u043e\u0434\u0436\u0438\u0434\u0430\u0442\u044c \u043f\u0440\u0438 \u0435\u0433\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438, \u043f\u0435\u0440\u0435\u0439\u0434\u0435\u043c \u043a \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u043d\u0438\u044e \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0438\u0437 \u0435\u0433\u043e \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0445 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0439.<\/p>\n<h3>\u0414\u0435\u0434\u0443\u043f\u043b\u0438\u043a\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430<\/h3>\n<p>\u042d\u0442\u043e \u043f\u0435\u0440\u0432\u043e\u0435 \u0438 \u0441\u0430\u043c\u043e\u0435 \u043e\u0447\u0435\u0432\u0438\u0434\u043d\u043e\u0435. \u041f\u043e\u043c\u0438\u043c\u043e \u0443\u0436\u0435 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u043d\u043d\u044b\u0445 \u043f\u0440\u0438\u043c\u0435\u0440\u043e\u0432, \u043f\u0440\u0438\u0432\u0435\u0434\u0443 \u0435\u0449\u0435 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/3d7\/e13\/9c9\/3d7e139c9f5f99651c9ea5f692ff02c5.png\" alt=\"\" title=\"\" width=\"960\" height=\"1246\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/3d7\/e13\/9c9\/3d7e139c9f5f99651c9ea5f692ff02c5.png\"\/><\/figure>\n<h3>\u0412\u043d\u0435\u0434\u0440\u0435\u043d\u0438\u0435 \u043c\u0435\u0442\u043e\u0434\u043e\u0432 \u0432 \u043a\u043b\u0430\u0441\u0441\u044b-\u043d\u0430\u0441\u043b\u0435\u0434\u043d\u0438\u043a\u0438<\/h3>\n<p>\u0414\u0430-\u0434\u0430, \u0432\u044b \u043f\u043e\u0434\u0443\u043c\u0430\u043b\u0438 \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e. CRTP (<em>Curiously Recurring Template Pattern<\/em>) \u0431\u043e\u043b\u044c\u0448\u0435 \u043d\u0435 \u043d\u0443\u0436\u0435\u043d. \u0418 \u0445\u043e\u0442\u044c \u043a\u043e\u0434 \u043e\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f <em>deducing this<\/em> \u0432 \u043f\u0440\u043e\u0441\u0442\u0435\u0439\u0448\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043d\u0438\u0436\u0435 \u043d\u0435 \u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0441\u044f \u043a\u043e\u0440\u043e\u0447\u0435, \u043d\u043e \u043e\u0447\u0435\u0432\u0438\u0434\u043d\u043e \u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0441\u044f \u0431\u043e\u043b\u0435\u0435 \u043f\u0440\u043e\u0441\u0442\u044b\u043c \u0438 \u0438\u043d\u0442\u0443\u0438\u0442\u0438\u0432\u043d\u043e \u043f\u043e\u043d\u044f\u0442\u043d\u044b\u043c.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/e11\/43c\/083\/e1143c0838b15915f05eb084b79b6df9.png\" alt=\"\" title=\"\" width=\"798\" height=\"287\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/e11\/43c\/083\/e1143c0838b15915f05eb084b79b6df9.png\"\/><\/figure>\n<h3>\u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u044b\u0435 \u043b\u044f\u043c\u0431\u0434\u044b<\/h3>\n<p>\u041e \u0447\u0435\u043c \u044f \u0440\u0430\u043d\u0435\u0435 \u0435\u0449\u0435 \u043d\u0435 \u0443\u043f\u043e\u043c\u0438\u043d\u0430\u043b \u2014 \u044d\u0442\u043e \u0442\u043e, \u0447\u0442\u043e \u0442\u0435\u043f\u0435\u0440\u044c \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0442\u044c \u043b\u044f\u043c\u0431\u0434\u044b \u0441 \u044f\u0432\u043d\u044b\u043c \u043e\u0431\u044a\u0435\u043a\u0442\u043d\u044b\u043c \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u043c, \u0431\u043b\u0430\u0433\u043e\u0434\u0430\u0440\u044f \u0447\u0435\u043c\u0443 \u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0441\u044f \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u043c \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u044b\u0445 \u043b\u044f\u043c\u0431\u0434:<\/p>\n<pre><code class=\"cpp\">auto fib = [](this auto self, int n) {     if (n &lt; 2) return n;     return self(n-1) + self(n-2); };<\/code><\/pre>\n<pre><code class=\"cpp\">struct Leaf { }; struct Node; using Tree = variant&lt;Leaf, Node*>; struct Node {     Tree left;     Tree right; };  int num_leaves(Tree const&amp; tree) {     return visit(overload(        \/\/ &lt;-----------------------------------+         [](Leaf const&amp;) { return 1; },                           \/\/      |         [](this auto const&amp; self, Node* n) -> int {              \/\/      |             return visit(self, n->left) + visit(self, n->right); \/\/ &lt;----+         }     ), tree); }<\/code><\/pre>\n<h3>\u041f\u0435\u0440\u0435\u0434\u0430\u0447\u0430 self \u043f\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044e<\/h3>\n<p>\u041f\u0435\u0440\u0435\u0434\u0430\u0447\u0430 <code>self<\/code> \u043f\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044e \u043e\u0442\u043a\u0440\u044b\u0432\u0430\u0435\u0442 \u0434\u043b\u044f \u043d\u0430\u0441 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0431\u043e\u043b\u0435\u0435 \u0435\u0441\u0442\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0433\u043e \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u0436\u0435\u043b\u0430\u0435\u043c\u043e\u0439 \u0441\u0435\u043c\u0430\u043d\u0442\u0438\u043a\u0438, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u043a\u043e\u0433\u0434\u0430 \u0441\u043c\u044b\u0441\u043b \u043c\u0435\u0442\u043e\u0434\u0430 \u0437\u0430\u043a\u043b\u044e\u0447\u0430\u0435\u0442\u0441\u044f \u0432 \u043e\u0434\u043d\u043e\u043c \u043b\u0438\u0448\u044c \u0432\u043e\u0437\u0432\u0440\u0430\u0442\u0435 \u043c\u043e\u0434\u0438\u0444\u0438\u0446\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0439 \u043a\u043e\u043f\u0438\u0438:<\/p>\n<pre><code class=\"cpp\">struct my_vector : vector&lt;int> {   auto sorted(this my_vector self) -> my_vector {     sort(self.begin(), self.end());     return self;   } };<\/code><\/pre>\n<p>\u041d\u043e, \u0447\u0442\u043e \u043c\u043d\u043e\u0433\u0438\u0435 \u0441\u043f\u0440\u0430\u0432\u0435\u0434\u043b\u0438\u0432\u043e \u043f\u043e\u0441\u0447\u0438\u0442\u0430\u044e\u0442 \u0431\u043e\u043b\u0435\u0435 \u0432\u0430\u0436\u043d\u044b\u043c \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u0435\u043c, \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0432 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0441\u043b\u0443\u0447\u0430\u044f\u0445 \u0434\u043e\u0441\u0442\u0438\u0447\u044c \u043b\u0443\u0447\u0448\u0435\u0439 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438.<\/p>\n<p>\u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0432\u0441\u0435 \u043c\u044b \u0437\u043d\u0430\u0435\u043c, \u0447\u0442\u043e \u043c\u0430\u043b\u0435\u043d\u044c\u043a\u0438\u0435 \u0442\u0438\u043f\u044b \u0432\u043e \u0438\u0437\u0431\u0435\u0436\u0430\u043d\u0438\u0435 \u043f\u043e\u0440\u043e\u0436\u0434\u0435\u043d\u0438\u044f \u043b\u0438\u0448\u043d\u0438\u0445 \u0443\u0440\u043e\u0432\u043d\u0435\u0439 \u043a\u043e\u0441\u0432\u0435\u043d\u043d\u043e\u0441\u0442\u0438 (<em>indirection<\/em>) \u043b\u0443\u0447\u0448\u0435 \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0442\u044c \u043f\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044e.<\/p>\n<p>\u041a \u043e\u0434\u043d\u043e\u043c\u0443 \u0438\u0437 \u0442\u0430\u043a\u0438\u0445 \u0442\u0438\u043f\u043e\u0432 \u043e\u0442\u043d\u043e\u0441\u0438\u0442\u0441\u044f <code>std::string_view<\/code>. \u041a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0442\u044c \u043f\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044e \u0432\u0441\u044e\u0434\u0443: \u0432 \u043d\u0430\u0448\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\u044b, \u0434\u0440\u0443\u0433\u0438\u0435 \u043c\u0435\u0442\u043e\u0434\u044b. \u041d\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0435 \u0432 \u0435\u0433\u043e \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0435 \u043c\u0435\u0442\u043e\u0434\u044b. \u0414\u043e \u043f\u0440\u0438\u043d\u044f\u0442\u0438\u044f <em>deducing this<\/em> \u0443 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u043e\u0432 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u043e\u0439 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 \u043d\u0435 \u0431\u044b\u043b\u043e \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u0432 \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u0440\u0430\u0437\u044b\u043c\u0435\u043d\u043e\u0432\u0430\u043d\u0438\u0439 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f <code>this<\/code> \u0432 \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0445 \u043c\u0435\u0442\u043e\u0434\u0430\u0445 <code>std::string_view<\/code>.<\/p>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0436\u0435 \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043f\u0435\u0440\u0435\u043f\u0438\u0441\u0430\u0442\u044c \u0432\u0441\u0435 \u0435\u0433\u043e \u043c\u0435\u0442\u043e\u0434\u044b, \u043d\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u044e\u0449\u0438\u0435 \u043c\u043e\u0434\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0439, \u0441 \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0435\u0439 \u044f\u0432\u043d\u043e\u0433\u043e \u043e\u0431\u044a\u0435\u043a\u0442\u043d\u043e\u0433\u043e \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430 \u043f\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044e, \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u0431\u0435\u0441\u043f\u043b\u0430\u0442\u043d\u043e \u044d\u0442\u0438\u043c \u043f\u043e\u043b\u0443\u0447\u0430\u044f \u0443\u043b\u0443\u0447\u0448\u0435\u043d\u0438\u0435 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438:<\/p>\n<pre><code class=\"cpp\">template &lt;class charT, class traits = char_traits&lt;charT>> class basic_string_view { private:     const_pointer data_;     size_type size_; public:     constexpr const_iterator begin(this basic_string_view self) {         return self.data_;     }      constexpr const_iterator end(this basic_string_view self) {         return self.data_ + self.size_;     }      constexpr size_t size(this basic_string_view self) {         return self.size_;     }      constexpr const_reference operator[](this basic_string_view self, size_type pos) {         return self.data_[pos];     } };<\/code><\/pre>\n<h2>\u0417\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/h2>\n<p>\u041d\u0430 \u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435, \u044d\u0442\u043e \u0434\u0430\u043b\u0435\u043a\u043e \u043d\u0435 \u0432\u0441\u0435, \u0447\u0442\u043e \u043c\u043e\u0436\u043d\u043e \u0441\u043a\u0430\u0437\u0430\u0442\u044c \u043f\u0440\u043e <em>deducing this<\/em>, \u043d\u043e \u0441\u0430\u043c\u043e\u0435 \u0433\u043b\u0430\u0432\u043d\u043e\u0435 \u0438 \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u0435. \u0426\u0435\u043b\u044c\u044e \u0434\u0430\u043d\u043d\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0438 \u043d\u0435 \u044f\u0432\u043b\u044f\u043b\u043e\u0441\u044c \u0443\u0433\u043b\u0443\u0431\u043b\u0435\u043d\u0438\u0435 \u0432 \u0434\u0435\u0442\u0430\u043b\u0438.<\/p>\n<p>\u0415\u0441\u043b\u0438 \u0432\u044b \u0445\u043e\u0442\u0438\u0442\u0435 \u043f\u043e\u0441\u0442\u0438\u0447\u044c \u0432\u0441\u0435 \u043d\u044e\u0430\u043d\u0441\u044b \u2014 \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u043e\u0431\u0440\u0430\u0442\u0438\u0442\u044c\u0441\u044f \u043a <a href=\"https:\/\/www.open-std.org\/jtc1\/sc22\/wg21\/docs\/papers\/2021\/p0847r6.html\" rel=\"noopener noreferrer nofollow\">\u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b\u044c\u043d\u043e\u043c\u0443 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0443<\/a>.<\/p>\n<p>\u0422\u0430\u043a\u0436\u0435 \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u044b \u0438 \u0443\u0432\u043b\u0435\u043a\u0430\u0442\u0435\u043b\u044c\u043d\u044b \u0441\u0442\u0430\u0442\u044c\u0438, \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u043d\u044b\u0435 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u043c\u0438 \u0438\u0437 \u0435\u0433\u043e \u0430\u0432\u0442\u043e\u0440\u043e\u0432: <a href=\"https:\/\/devblogs.microsoft.com\/cppblog\/cpp23-deducing-this\/\" rel=\"noopener noreferrer nofollow\"><em>\u00abC++23\u2019s Deducing this: what it is, why it is, how to use it\u00bb<\/em><\/a> (Sy Brand), <a href=\"https:\/\/brevzin.github.io\/about\/\" rel=\"noopener noreferrer nofollow\"><em>\u00abCopy-on-write with Deducing this\u00bb<\/em><\/a> (Barry Revzin).<\/p>\n<p>\u041b\u044e\u0431\u0438\u0442\u0435 \u043f\u043b\u044e\u0441\u044b \u0438 \u0431\u0443\u0434\u044c\u0442\u0435 \u0441\u0447\u0430\u0441\u0442\u043b\u0438\u0432\u044b.<\/p>\n<\/p>\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\/post\/722668\/\"> https:\/\/habr.com\/ru\/post\/722668\/<\/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<p>\u0414\u043e\u043a\u0443\u043c\u0435\u043d\u0442 <em>\u00abdeducing this\u00bb<\/em>, \u043f\u0440\u0438\u043d\u044f\u0442\u044b\u0439 \u0432 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0439 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442 C++, \u0432\u0432\u043e\u0434\u0438\u0442 \u043d\u043e\u0432\u044b\u0439, \u0442\u0440\u0435\u0442\u0438\u0439 \u0442\u0438\u043f \u043c\u0435\u0442\u043e\u0434\u043e\u0432 \u043a\u043b\u0430\u0441\u0441\u043e\u0432, \u0441\u043e\u0447\u0435\u0442\u0430\u044e\u0449\u0438\u0439 \u0432 \u0441\u0435\u0431\u0435 \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u0430 \u0434\u0432\u0443\u0445 \u0443\u0436\u0435 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0445: <em>\u043d\u0435\u0441\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0445<\/em> \u0438 <em>\u0441\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0445<\/em>, \u043e\u0442\u043a\u0440\u044b\u0432\u0430\u044e\u0449\u0438\u0439 \u043f\u0435\u0440\u0435\u0434 \u043d\u0430\u043c\u0438 \u043d\u043e\u0432\u044b\u0435 \u0433\u043e\u0440\u0438\u0437\u043e\u043d\u0442\u044b:<\/p>\n<ol>\n<li>\n<p>\u0414\u0435\u0434\u0443\u043f\u043b\u0438\u043a\u0430\u0446\u0438\u044f \u0431\u043e\u043b\u044c\u0448\u043e\u0433\u043e \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0430 \u043a\u043e\u0434\u0430<\/p>\n<\/li>\n<li>\n<p>\u0412\u044b\u0442\u0435\u0441\u043d\u0435\u043d\u0438\u0435 CRTP (<em>Curiously Recuring Template Pattern<\/em>) \u043d\u0430 \u0441\u0432\u0430\u043b\u043a\u0443 \u0438\u0441\u0442\u043e\u0440\u0438\u0438, \u0435\u0433\u043e \u0437\u0430\u043c\u0435\u043d\u0430 \u0431\u043e\u043b\u0435\u0435 \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u0438 \u043e\u0447\u0435\u0432\u0438\u0434\u043d\u043e \u043f\u043e\u043d\u044f\u0442\u043d\u043e\u0439 \u0437\u0430\u043f\u0438\u0441\u044c\u044e<\/p>\n<\/li>\n<li>\n<p>\u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u044b\u0435 \u043b\u044f\u043c\u0431\u0434\u044b<\/p>\n<\/li>\n<\/ol>\n<p>\u0418 \u0434\u0440\u0443\u0433\u043e\u0435.<\/p>\n<p>\u041d\u043e \u043f\u0440\u0435\u0436\u0434\u0435 \u0447\u0435\u043c \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u0441\u0430\u043c\u043e \u043d\u043e\u0432\u043e\u0432\u0432\u0435\u0434\u0435\u043d\u0438\u0435 \u0438 \u0435\u0433\u043e \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0435 \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u044f, \u0443\u0433\u043b\u0443\u0431\u0438\u043c\u0441\u044f \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u0432 \u0438\u0441\u0442\u043e\u0440\u0438\u044e \u0438 \u043f\u043e\u043f\u044b\u0442\u0430\u0435\u043c\u0441\u044f \u043f\u043e\u043d\u044f\u0442\u044c, \u043f\u043e\u0447\u0435\u043c\u0443 \u0432 \u043d\u0435\u043c \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u0432\u043e\u0437\u043d\u0438\u043a\u043b\u0430 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u044c.<\/p>\n<h2>\u041c\u043e\u0442\u0438\u0432\u0430\u0446\u0438\u044f<\/h2>\n<p>\u041d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 C++03, \u043c\u0435\u0442\u043e\u0434\u044b \u043c\u043e\u0433\u0443\u0442 \u0438\u043c\u0435\u0442\u044c <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/language\/cv\" rel=\"noopener noreferrer nofollow\"><strong><em>cv-\u043a\u0432\u0430\u043b\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440\u044b<\/em><\/strong><\/a>, \u0442\u0430\u043a \u0447\u0442\u043e \u0441\u0442\u0430\u043b\u0438 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u0438, \u043a\u043e\u0433\u0434\u0430 \u0435\u0441\u0442\u044c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u044c \u043a\u0430\u043a \u0432 <code>const<\/code>, \u0442\u0430\u043a \u0438 \u043d\u0435-<code>const<\/code> \u043f\u0435\u0440\u0435\u0433\u0440\u0443\u0437\u043a\u0435 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u043e\u0433\u043e \u043c\u0435\u0442\u043e\u0434\u0430 (\u0434\u043b\u044f \u043a\u0440\u0430\u0442\u043a\u043e\u0441\u0442\u0438 \u0432\u043e\u0437\u0434\u0435\u0440\u0436\u0438\u043c\u0441\u044f \u043e\u0442 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u043d\u0438\u044f <code>volatile<\/code> \u043f\u0435\u0440\u0435\u0433\u0440\u0443\u0437\u043e\u043a).<\/p>\n<p>\u0412\u043e \u043c\u043d\u043e\u0433\u0438\u0445 \u0441\u043b\u0443\u0447\u0430\u044f\u0445 \u043c\u0435\u0436\u0434\u0443 \u0438\u0445 \u043b\u043e\u0433\u0438\u043a\u043e\u0439 \u043d\u0435\u0442 \u043d\u0438\u043a\u0430\u043a\u043e\u0439 \u0440\u0430\u0437\u043d\u0438\u0446\u044b \u2014 \u043e\u0442\u043b\u0438\u0447\u0430\u044e\u0442\u0441\u044f \u043b\u0438\u0448\u044c <em>\u043a\u0432\u0430\u043b\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u044b\u0445 \u0442\u0438\u043f\u043e\u0432<\/em>, \u0442\u0430\u043a \u0447\u0442\u043e \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0438\u043b\u0438 \u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435, \u043f\u043e\u0434\u0433\u043e\u043d\u044f\u044f \u043a\u0432\u0430\u043b\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440\u044b, \u0438\u043b\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0442\u0430\u043a\u0438\u0435 \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c\u044b \u043a\u0430\u043a <code>const_cast<\/code>:<\/p>\n<pre><code class=\"cpp\">class TextBlock { public:   char const&amp; operator[](size_t position) const {     \/\/ ...     return text[position];   }    char&amp; operator[](size_t position) {     return const_cast&lt;char&amp;>(       static_cast&lt;TextBlock const&amp;>(*this)[position]     );   }   \/\/ ... };<\/code><\/pre>\n<p>\u041d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 C++11, \u043c\u0435\u0442\u043e\u0434\u044b \u043c\u043e\u0433\u0443\u0442 \u0438\u043c\u0435\u0442\u044c \u0442\u0430\u043a\u0436\u0435 \u0438 <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/language\/member_functions#member_functions_with_ref-qualifier\" rel=\"noopener noreferrer nofollow\"><strong><em>ref-\u043a\u0432\u0430\u043b\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440\u044b<\/em><\/strong><\/a>, \u0442\u0430\u043a \u0447\u0442\u043e \u0442\u0435\u043f\u0435\u0440\u044c \u0432\u043c\u0435\u0441\u0442\u043e \u0434\u0432\u0443\u0445 \u043f\u0435\u0440\u0435\u0433\u0440\u0443\u0437\u043e\u043a \u043e\u0434\u043d\u043e\u0433\u043e \u043c\u0435\u0442\u043e\u0434\u0430 \u043d\u0430\u043c \u043c\u043e\u0433\u0443\u0442 \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u0442\u044c\u0441\u044f \u0447\u0435\u0442\u044b\u0440\u0435: <code>&amp;<\/code>, <code>const&amp;<\/code>, <code>&amp;&amp;<\/code>, <code>const&amp;&amp;<\/code>, \u0438 \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u0442\u0440\u0438 \u0441\u043f\u043e\u0441\u043e\u0431\u0430 \u0440\u0435\u0448\u0438\u0442\u044c \u0434\u0430\u043d\u043d\u0443\u044e \u0437\u0430\u0434\u0430\u0447\u0443 (<a href=\"https:\/\/godbolt.org\/z\/Pqvn3rKvh\" rel=\"noopener noreferrer nofollow\">\u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043a\u043e\u0434, \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d\u043d\u044b\u0439 \u043d\u0438\u0436\u0435<\/a>):<\/p>\n<ol>\n<li>\n<p>\u041f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e \u043e\u0434\u043d\u043e\u0433\u043e \u043c\u0435\u0442\u043e\u0434\u0430 \u0447\u0435\u0442\u044b\u0440\u0435\u0436\u0434\u044b<\/p>\n<\/li>\n<li>\n<p>\u0414\u0435\u043b\u0435\u0433\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0442\u0440\u0438 \u043f\u0435\u0440\u0435\u0433\u0440\u0443\u0437\u043a\u0438 \u0447\u0435\u0442\u0432\u0435\u0440\u0442\u043e\u0439, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f <code>static_cast<\/code> \u0438 <code>const_cast<\/code><\/p>\n<\/li>\n<li>\n<p>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u0443\u044e \u0448\u0430\u0431\u043b\u043e\u043d\u043d\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e<\/p>\n<\/li>\n<\/ol>\n<figure class=\"full-width\"><\/figure>\n<p>\u041d\u043e \u043d\u0438 \u043e\u0434\u0438\u043d \u0438\u0437 \u044d\u0442\u0438\u0445 \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u0432 \u043d\u0435 \u0438\u0437\u0431\u0430\u0432\u043b\u044f\u0435\u0442 \u043d\u0430\u0441 \u043e\u0442 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u0438 \u0447\u0435\u0442\u044b\u0440\u0435\u0436\u0434\u044b \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0442\u044c \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u043e\u0434\u0438\u043d \u0438 \u0442\u043e\u0442 \u0436\u0435 \u043c\u0435\u0442\u043e\u0434.<\/p>\n<p>\u0415\u0441\u043b\u0438 \u0431\u044b \u043c\u043e\u0433\u043b\u0438 \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0447\u0442\u043e-\u0442\u043e \u0432\u0440\u043e\u0434\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043d\u0438\u0436\u0435, \u043d\u043e \u0432\u0435\u0434\u0443\u0449\u0435\u0439 \u0441\u0435\u0431\u044f \u043a\u0430\u043a \u0447\u043b\u0435\u043d \u043a\u043b\u0430\u0441\u0441\u0430, \u044d\u0442\u043e \u0440\u0435\u0448\u0438\u043b\u043e \u0431\u044b \u0432\u0441\u0435 \u043d\u0430\u0448\u0438 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b:<\/p>\n<pre><code class=\"cpp\">template &lt;typename T> class optional {     \/\/ ...     template &lt;typename Opt>     friend decltype(auto) value(Opt&amp;&amp; o) {         if (o.has_value()) {             return forward&lt;Opt>(o).m_value;         }         throw bad_optional_access();     }     \/\/ ... };<\/code><\/pre>\n<p>\u041d\u043e \u043c\u044b \u043d\u0435 \u043c\u043e\u0436\u0435\u043c. \u0422\u043e\u0447\u043d\u0435\u0439, <em>\u043d\u0435 \u043c\u043e\u0433\u043b\u0438<\/em>. \u0414\u043e C++23<\/p>\n<h2>\u041c\u0435\u0447\u0442\u044b \u0432\u043e\u043f\u043b\u043e\u0449\u0430\u044e\u0442\u0441\u044f \u0432 \u0440\u0435\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u044c<\/h2>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0436\u0435 \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0442\u044c \u043c\u0435\u0442\u043e\u0434\u044b, \u044f\u0432\u043d\u043e \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u044e\u0449\u0438\u0435 \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0430 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u0430 \u043e\u0431\u044a\u0435\u043a\u0442, \u043d\u0430\u0434 \u043a\u043e\u0442\u043e\u0440\u044b\u043c \u043e\u043d\u0438 \u0431\u044b\u043b\u0438 \u0432\u044b\u0437\u0432\u0430\u043d\u044b:<\/p>\n<pre><code class=\"cpp\">struct X {     template &lt;typename Self>     void foo(this Self&amp;&amp;) { } };  void example(X&amp; x) {     x.foo();            \/\/ Self = X&amp;     move(x).foo();      \/\/ Self = X     X{}.foo();          \/\/ Self = X }<\/code><\/pre>\n<p>\u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, \u0442\u0435\u043f\u0435\u0440\u044c \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043f\u0435\u0440\u0435\u043f\u0438\u0441\u0430\u0442\u044c \u0432\u0441\u044e \u0442\u0443 \u043f\u0440\u043e\u0441\u0442\u044b\u043d\u044e \u043a\u043e\u0434\u0430, \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u044e\u0449\u0443\u044e \u043c\u0435\u0442\u043e\u0434 <code>value<\/code> \u0443 <code>optional<\/code>, \u0433\u043e\u0440\u0430\u0437\u0434\u043e \u0431\u043e\u043b\u0435\u0435 \u043a\u043e\u043c\u043f\u0430\u043a\u0442\u043d\u043e, \u043c\u0435\u043d\u044c\u0448\u0435 \u0447\u0435\u043c \u0432 \u0434\u0435\u0441\u044f\u0442\u043e\u043a \u0441\u0442\u0440\u043e\u043a:<\/p>\n<pre><code class=\"cpp\">template &lt;typename T> struct optional {   template &lt;typename Self>   constexpr auto&amp;&amp; value(this Self&amp;&amp; self) {     if (!self.has_value()) {       throw bad_optional_access();     }     return forward&lt;Self>(self).m_value; }<\/code><\/pre>\n<p>\u041c\u044b \u043f\u043e\u0434\u0447\u0438\u043d\u0438\u043b\u0438 \u0441\u0432\u043e\u0438\u043c \u0446\u0435\u043b\u044f\u043c \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c \u0432\u044b\u0432\u043e\u0434\u0430 \u0442\u0438\u043f\u043e\u0432 \u0432\u043e \u0432\u0441\u0435\u0439 \u0441\u0432\u043e\u0435\u0439 \u043c\u043e\u0449\u0438! \u0418 \u0432\u0430\u0436\u043d\u043e \u043e\u0442\u043c\u0435\u0442\u0438\u0442\u044c, \u0447\u0442\u043e \u044d\u0442\u043e <em>\u0432\u0441\u0435 \u0442\u043e\u0442 \u0436\u0435 \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c<\/em>, \u043f\u0440\u043e\u044f\u0432\u043b\u044f\u044e\u0449\u0438\u0439 \u0441\u0435\u0431\u044f \u0432 \u043e\u0431\u044b\u0447\u043d\u044b\u0445 \u0448\u0430\u0431\u043b\u043e\u043d\u043d\u044b\u0445 \u0444\u0443\u043d\u043a\u0446\u0438\u044f\u0445 \u0438 \u043c\u0435\u0442\u043e\u0434\u0430\u0445.<\/p>\n<p><em>Deducing this<\/em> \u043d\u0435 \u0432\u043d\u043e\u0441\u0438\u0442 \u043d\u0438\u043a\u0430\u043a\u0438\u0445 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0439 \u0432 \u043f\u0440\u0430\u0432\u0438\u043b\u0430 \u0432\u044b\u0432\u043e\u0434\u0430 \u0442\u0438\u043f\u043e\u0432, \u043e\u043d \u043b\u0438\u0448\u044c \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u044f\u0432\u043d\u043e\u0435 \u043e\u0431\u044a\u044f\u0432\u043b\u0435\u043d\u0438\u0435 <em>\u043e\u0431\u044a\u0435\u043a\u0442\u043d\u043e\u0433\u043e \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430<\/em> (<em>explicit object parameter<\/em>) \u0432 \u0441\u043f\u0438\u0441\u043a\u0435 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u0432 \u043c\u0435\u0442\u043e\u0434\u043e\u0432. \u041f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0434\u043e \u044d\u0442\u043e\u0433\u043e \u0432 \u043c\u0435\u0442\u043e\u0434\u0430\u0445 \u043f\u0440\u0438\u0441\u0443\u0442\u0441\u0442\u0432\u043e\u0432\u0430\u043b \u043b\u0438\u0448\u044c <em>\u043d\u0435\u044f\u0432\u043d\u043e<\/em> (<em>implicit object parameter<\/em>), \u0432 \u0432\u0438\u0434\u0435 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f <code>this<\/code>.<\/p>\n<p>\u0412\u0430\u0436\u043d\u043e \u043e\u0442\u043c\u0435\u0442\u0438\u0442\u044c, \u0447\u0442\u043e \u0432\u044b\u0432\u043e\u0434 \u0442\u0438\u043f\u043e\u0432 \u0441\u043f\u043e\u0441\u043e\u0431\u0435\u043d \u0432\u044b\u0432\u043e\u0434\u0438\u0442\u044c \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u043d\u044b\u0435 \u0442\u0438\u043f\u044b:<\/p>\n<pre><code class=\"cpp\">struct X {   template &lt;typename Self>   void foo(this Self&amp;&amp;, int); };  struct D : X { };  void example(X&amp; x, D&amp; d) {     x.foo(1);               \/\/ Self=X&amp;     move(x).foo(2);         \/\/ Self=X     d.foo(3);               \/\/ Self=D&amp; }<\/code><\/pre>\n<p>\u041d\u0430 \u043c\u0435\u0442\u043e\u0434\u044b, \u043e\u0431\u044a\u044f\u0432\u043b\u0435\u043d\u043d\u044b\u0435 \u0441 \u044f\u0432\u043d\u044b\u043c \u043e\u0431\u044a\u0435\u043a\u0442\u043d\u044b\u043c \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u043c, \u0442\u0430\u043a\u0436\u0435 \u043d\u0430\u043a\u043b\u0430\u0434\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0440\u044f\u0434 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0439:<\/p>\n<ol>\n<li>\n<p><strong>\u041e\u043d\u0438 \u043d\u0435 \u043c\u043e\u0433\u0443\u0442 \u0431\u044b\u0442\u044c \u043e\u0431\u044a\u044f\u0432\u043b\u0435\u043d\u044b \u0441\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u043c\u0438<\/strong><\/p>\n<\/li>\n<\/ol>\n<p>\u041f\u0440\u0438\u0447\u0438\u043d\u0430 \u044d\u0442\u043e\u0433\u043e \u0437\u0430\u043a\u043b\u044e\u0447\u0430\u0435\u0442\u0441\u044f \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e \u0445\u043e\u0442\u044c \u0438 \u0432\u043d\u0435\u0448\u043d\u0435 \u044d\u0442\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0432\u044b\u0433\u043b\u044f\u0434\u044f\u0442 \u0438 \u0432\u0435\u0434\u0443\u0442 \u0441\u0435\u0431\u044f \u043a\u0430\u043a \u043e\u0431\u044b\u0447\u043d\u044b\u0435 \u043d\u0435\u0441\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0435 \u043c\u0435\u0442\u043e\u0434\u044b, \u043d\u043e \u0432\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0435 \u043e\u043d\u0438 \u0432\u0435\u0434\u0443\u0442 \u0441\u0435\u0431\u044f \u0432 \u0442\u043e\u0447\u043d\u043e\u0441\u0442\u0438 \u043a\u0430\u043a \u0441\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0435: \u0432 \u043d\u0438\u0445 <strong>\u043d\u0435\u0434\u043e\u0441\u0442\u0443\u043f\u0435\u043d \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c <\/strong><code>this<\/code>, \u0438 \u0435\u0434\u0438\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0439 \u0441\u043f\u043e\u0441\u043e\u0431 \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u043e\u0432\u0430\u0442\u044c \u0432 \u043d\u0438\u0445 \u0441 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u043c \u043a\u043b\u0430\u0441\u0441\u0430 \u2014 \u0447\u0435\u0440\u0435\u0437 \u044f\u0432\u043d\u044b\u0439 \u043e\u0431\u044a\u0435\u043a\u0442\u043d\u044b\u0439 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440.<\/p>\n<p>\u041a\u0440\u043e\u043c\u0435 \u0442\u043e\u0433\u043e, <strong>\u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u043d\u0430 \u0442\u0430\u043a\u0438\u0435 \u043c\u0435\u0442\u043e\u0434\u044b \u2014 \u044d\u0442\u043e \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u043d\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u044e<\/strong>, \u0430 \u043d\u0435 \u0447\u043b\u0435\u043d \u043a\u043b\u0430\u0441\u0441\u0430.<\/p>\n<ol start=\"2\">\n<li>\n<p><strong>\u041e\u043d\u0438 \u043d\u0435 \u043c\u043e\u0433\u0443\u0442 \u0431\u044b\u0442\u044c \u043e\u0431\u044a\u044f\u0432\u043b\u0435\u043d\u044b \u0432\u0438\u0440\u0442\u0443\u0430\u043b\u044c\u043d\u044b\u043c\u0438<\/strong><\/p>\n<\/li>\n<li>\n<p><strong>\u041e\u043d\u0438 \u043d\u0435 \u043c\u043e\u0433\u0443\u0442 \u0431\u044b\u0442\u044c \u043e\u0431\u044a\u044f\u0432\u043b\u0435\u043d\u044b \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c <em>ref \u0438\u043b\u0438 cv \u043a\u0432\u0430\u043b\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440\u043e\u0432<\/em><\/strong><\/p>\n<\/li>\n<\/ol>\n<p>\u0422\u0430\u043a \u043a\u0430\u043a \u043e\u0431\u044a\u044f\u0432\u043b\u0435\u043d\u0438\u0435 \u044f\u0432\u043d\u043e\u0433\u043e \u043e\u0431\u044a\u0435\u043a\u0442\u043d\u043e\u0433\u043e \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430 \u0443\u0436\u0435 \u043d\u0435\u0441\u0435\u0442 \u0432 \u0441\u0435\u0431\u0435 \u0432\u0441\u044e \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u0443\u044e \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u043e \u0435\u0433\u043e \u0442\u0438\u043f\u0435:<\/p>\n<figure class=\"full-width\"><\/figure>\n<h2>\u041d\u043e \u0435\u0441\u0442\u044c \u043d\u044e\u0430\u043d\u0441\u044b<\/h2>\n<p>\u041e\u0434\u043d\u0430\u043a\u043e \u0441 \u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u0441\u0438\u043b\u043e\u0439 \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442 \u0438 \u0431\u043e\u043b\u044c\u0448\u0430\u044f \u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0441\u0442\u044c. \u0422\u0430\u043a, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u043d\u043e\u0432\u044b\u0439 \u0442\u0438\u043f \u043c\u0435\u0442\u043e\u0434\u043e\u0432, \u043c\u044b \u0434\u043e\u043b\u0436\u043d\u044b \u043f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u043e \u043f\u043e\u043c\u043d\u0438\u0442\u044c \u043e \u0442\u043e\u043c, \u043d\u0430\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043c\u043e\u0433\u0443\u0449\u0435\u0441\u0442\u0432\u0435\u043d \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c \u0432\u044b\u0432\u043e\u0434\u0430 \u0442\u0438\u043f\u043e\u0432:<\/p>\n<pre><code class=\"cpp\">struct B {     int i = 0;      template &lt;typename Self> auto&amp;&amp; f1(this Self&amp;&amp; self) { return forward&lt;Self>(self).i; } };  struct D: B {     double i = 3.14; };<\/code><\/pre>\n<p>\u0422\u043e\u0433\u0434\u0430 \u043a\u0430\u043a <code>B().f1()<\/code> \u0432 \u043a\u043e\u0434\u0435 \u0432\u044b\u0448\u0435 \u0432\u0435\u0440\u043d\u0435\u0442 \u0441\u0441\u044b\u043b\u043a\u0443 \u043d\u0430 <code>B::i<\/code>, <code>D().f5()<\/code> \u0432\u0435\u0440\u043d\u0435\u0442 \u0441\u0441\u044b\u043b\u043a\u0443 \u043d\u0430 <code>D::i<\/code>, \u0442\u0430\u043a \u043a\u0430\u043a <code>self<\/code> \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0441\u0441\u044b\u043b\u043a\u043e\u0439 \u043d\u0430 <code>D<\/code>.<\/p>\n<p>\u0415\u0441\u043b\u0438 \u0436\u0435 \u043c\u044b \u0445\u043e\u0442\u0438\u043c \u043f\u043e\u043b\u0443\u0447\u0430\u0442\u044c \u0441\u0441\u044b\u043b\u043a\u0443 \u043d\u0430 <code>B::i<\/code> \u0432\u0441\u0435\u0433\u0434\u0430, \u043d\u0430\u043c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u044f\u0432\u043d\u043e \u043f\u0440\u0435\u0434\u0443\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u044d\u0442\u043e \u0432 \u043a\u043e\u0434\u0435:<\/p>\n<pre><code class=\"cpp\">template &lt;typename Self> auto&amp;&amp; f1(this Self&amp;&amp; self) {     return forward&lt;Self>(self).B::i; }<\/code><\/pre>\n<p>\u0414\u0440\u0443\u0433\u043e\u0439 \u043e\u043f\u0430\u0441\u043d\u043e\u0441\u0442\u044c\u044e \u043d\u0430 \u043d\u0430\u0448\u0435\u043c \u043f\u0443\u0442\u0438 \u043c\u043e\u0436\u0435\u0442 \u0441\u043b\u0443\u0436\u0438\u0442\u044c \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 \u043f\u0440\u0438\u0432\u0430\u0442\u043d\u043e\u0433\u043e \u043d\u0430\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u043d\u0438\u044f. \u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u043a\u043e\u0434:<\/p>\n<pre><code class=\"cpp\">class B {     int i; public:     template &lt;typename Self>     auto&amp;&amp; get(this Self&amp;&amp; self) {         return forward&lt;Self>(self).B::i;     } };  class D: private B {     double i; public:     using B::get; };  D().get(); \/\/ error<\/code><\/pre>\n<p>\u041c\u044b, \u043a\u0430\u0437\u0430\u043b\u043e\u0441\u044c \u0431\u044b, \u043f\u0440\u0435\u0434\u043e\u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0441\u044c \u043a\u0430\u043a \u043c\u043e\u0433\u043b\u0438. \u041e\u0434\u043d\u0430\u043a\u043e \u043d\u0435\u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e. \u041c\u044b \u043d\u0435 \u043c\u043e\u0436\u0435\u043c \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0434\u043e\u0441\u0442\u0443\u043f \u043a <code>B::i<\/code> \u0438\u0437 <code>D<\/code>, \u0442\u0430\u043a \u043a\u0430\u043a \u043d\u0430\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u043d\u0438\u0435 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043f\u0440\u0438\u0432\u0430\u0442\u043d\u044b\u043c.<\/p>\n<p>\u041d\u043e \u0438 \u0434\u043b\u044f \u044d\u0442\u043e\u0439 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u0440\u0435\u0448\u0435\u043d\u0438\u0435:<\/p>\n<pre><code class=\"cpp\">class B {     int i; public:     template &lt;typename Self>     auto&amp;&amp; get(this Self&amp;&amp; self) {         \/\/ like_t \u2014 \u0444\u0443\u043d\u043a\u0446\u0438\u044f, \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u044e\u0449\u0430\u044f ref \u0438 cv \u043a\u0432\u0430\u043b\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440\u044b         \/\/ \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u043f\u0435\u0440\u0435\u0434\u0430\u043d\u043d\u043e\u0433\u043e \u0442\u0438\u043f\u0430 \u043a\u043e \u0432\u0442\u043e\u0440\u043e\u043c\u0443         \/\/ \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, like_t&lt;int&amp; double> = double&amp;         return ((like_t&lt;Self, B>&amp;&amp;)self).i;     } };  class D : private B {     double i; public:     using B::get; };  D().get(); \/\/ now ok, and returns B::i<\/code><\/pre>\n<p>\u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0432 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d\u0438\u0435 \u0432 \u0441\u0442\u0438\u043b\u0435 \u0441\u0438 \u0434\u043b\u044f \u0438\u0437\u0431\u0435\u0436\u0430\u043d\u0438\u044f \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u0434\u043e\u0441\u0442\u0443\u043f\u0430, \u043c\u044b \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043b\u0438 \u0436\u0435\u043b\u0430\u0435\u043c\u043e\u0435 \u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u0435.<\/p>\n<h2>\u041f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u0435<\/h2>\n<p>\u0418\u0442\u0430\u043a, \u043d\u0435\u043c\u043d\u043e\u0436\u043a\u043e \u043f\u043e\u0433\u043e\u0432\u043e\u0440\u0438\u0432 \u043e \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0438 \u043d\u043e\u0432\u043e\u0432\u0432\u0435\u0434\u0435\u043d\u0438\u044f \u0438 \u043e\u0431 \u043e\u043f\u0430\u0441\u043d\u043e\u0441\u0442\u044f\u0445, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u043e\u0433\u0443\u0442 \u043d\u0430\u0441 \u043f\u043e\u0434\u0436\u0438\u0434\u0430\u0442\u044c \u043f\u0440\u0438 \u0435\u0433\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438, \u043f\u0435\u0440\u0435\u0439\u0434\u0435\u043c \u043a \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u043d\u0438\u044e \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0438\u0437 \u0435\u0433\u043e \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0445 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0439.<\/p>\n<h3>\u0414\u0435\u0434\u0443\u043f\u043b\u0438\u043a\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430<\/h3>\n<p>\u042d\u0442\u043e \u043f\u0435\u0440\u0432\u043e\u0435 \u0438 \u0441\u0430\u043c\u043e\u0435 \u043e\u0447\u0435\u0432\u0438\u0434\u043d\u043e\u0435. \u041f\u043e\u043c\u0438\u043c\u043e \u0443\u0436\u0435 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u043d\u043d\u044b\u0445 \u043f\u0440\u0438\u043c\u0435\u0440\u043e\u0432, \u043f\u0440\u0438\u0432\u0435\u0434\u0443 \u0435\u0449\u0435 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e:<\/p>\n<figure class=\"full-width\"><\/figure>\n<h3>\u0412\u043d\u0435\u0434\u0440\u0435\u043d\u0438\u0435 \u043c\u0435\u0442\u043e\u0434\u043e\u0432 \u0432 \u043a\u043b\u0430\u0441\u0441\u044b-\u043d\u0430\u0441\u043b\u0435\u0434\u043d\u0438\u043a\u0438<\/h3>\n<p>\u0414\u0430-\u0434\u0430, \u0432\u044b \u043f\u043e\u0434\u0443\u043c\u0430\u043b\u0438 \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e. CRTP (<em>Curiously Recurring Template Pattern<\/em>) \u0431\u043e\u043b\u044c\u0448\u0435 \u043d\u0435 \u043d\u0443\u0436\u0435\u043d. \u0418 \u0445\u043e\u0442\u044c \u043a\u043e\u0434 \u043e\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f <em>deducing this<\/em> \u0432 \u043f\u0440\u043e\u0441\u0442\u0435\u0439\u0448\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043d\u0438\u0436\u0435 \u043d\u0435 \u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0441\u044f \u043a\u043e\u0440\u043e\u0447\u0435, \u043d\u043e \u043e\u0447\u0435\u0432\u0438\u0434\u043d\u043e \u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0441\u044f \u0431\u043e\u043b\u0435\u0435 \u043f\u0440\u043e\u0441\u0442\u044b\u043c \u0438 \u0438\u043d\u0442\u0443\u0438\u0442\u0438\u0432\u043d\u043e \u043f\u043e\u043d\u044f\u0442\u043d\u044b\u043c.<\/p>\n<figure class=\"full-width\"><\/figure>\n<h3>\u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u044b\u0435 \u043b\u044f\u043c\u0431\u0434\u044b<\/h3>\n<p>\u041e \u0447\u0435\u043c \u044f \u0440\u0430\u043d\u0435\u0435 \u0435\u0449\u0435 \u043d\u0435 \u0443\u043f\u043e\u043c\u0438\u043d\u0430\u043b \u2014 \u044d\u0442\u043e \u0442\u043e, \u0447\u0442\u043e \u0442\u0435\u043f\u0435\u0440\u044c \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0442\u044c \u043b\u044f\u043c\u0431\u0434\u044b \u0441 \u044f\u0432\u043d\u044b\u043c \u043e\u0431\u044a\u0435\u043a\u0442\u043d\u044b\u043c \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u043c, \u0431\u043b\u0430\u0433\u043e\u0434\u0430\u0440\u044f \u0447\u0435\u043c\u0443 \u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0441\u044f \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u043c \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u044b\u0445 \u043b\u044f\u043c\u0431\u0434:<\/p>\n<pre><code class=\"cpp\">auto fib = [](this auto self, int n) {     if (n &lt; 2) return n;     return self(n-1) + self(n-2); };<\/code><\/pre>\n<pre><code class=\"cpp\">struct Leaf { }; struct Node; using Tree = variant&lt;Leaf, Node*>; struct Node {     Tree left;     Tree right; };  int num_leaves(Tree const&amp; tree) {     return visit(overload(        \/\/ &lt;-----------------------------------+         [](Leaf const&amp;) { return 1; },                           \/\/      |         [](this auto const&amp; self, Node* n) -> int {              \/\/      |             return visit(self, n->left) + visit(self, n->right); \/\/ &lt;----+         }     ), tree); }<\/code><\/pre>\n<h3>\u041f\u0435\u0440\u0435\u0434\u0430\u0447\u0430 self \u043f\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044e<\/h3>\n<p>\u041f\u0435\u0440\u0435\u0434\u0430\u0447\u0430 <code>self<\/code> \u043f\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044e \u043e\u0442\u043a\u0440\u044b\u0432\u0430\u0435\u0442 \u0434\u043b\u044f \u043d\u0430\u0441 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0431\u043e\u043b\u0435\u0435 \u0435\u0441\u0442\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0433\u043e \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u0436\u0435\u043b\u0430\u0435\u043c\u043e\u0439 \u0441\u0435\u043c\u0430\u043d\u0442\u0438\u043a\u0438, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u043a\u043e\u0433\u0434\u0430 \u0441\u043c\u044b\u0441\u043b \u043c\u0435\u0442\u043e\u0434\u0430 \u0437\u0430\u043a\u043b\u044e\u0447\u0430\u0435\u0442\u0441\u044f \u0432 \u043e\u0434\u043d\u043e\u043c \u043b\u0438\u0448\u044c \u0432\u043e\u0437\u0432\u0440\u0430\u0442\u0435 \u043c\u043e\u0434\u0438\u0444\u0438\u0446\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0439 \u043a\u043e\u043f\u0438\u0438:<\/p>\n<pre><code class=\"cpp\">struct my_vector : vector&lt;int> {   auto sorted(this my_vector self) -> my_vector {     sort(self.begin(), self.end());     return self;   } };<\/code><\/pre>\n<p>\u041d\u043e, \u0447\u0442\u043e \u043c\u043d\u043e\u0433\u0438\u0435 \u0441\u043f\u0440\u0430\u0432\u0435\u0434\u043b\u0438\u0432\u043e \u043f\u043e\u0441\u0447\u0438\u0442\u0430\u044e\u0442 \u0431\u043e\u043b\u0435\u0435 \u0432\u0430\u0436\u043d\u044b\u043c \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u0435\u043c, \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0432 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0441\u043b\u0443\u0447\u0430\u044f\u0445 \u0434\u043e\u0441\u0442\u0438\u0447\u044c \u043b\u0443\u0447\u0448\u0435\u0439 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438.<\/p>\n<p>\u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0432\u0441\u0435 \u043c\u044b \u0437\u043d\u0430\u0435\u043c, \u0447\u0442\u043e \u043c\u0430\u043b\u0435\u043d\u044c\u043a\u0438\u0435 \u0442\u0438\u043f\u044b \u0432\u043e \u0438\u0437\u0431\u0435\u0436\u0430\u043d\u0438\u0435 \u043f\u043e\u0440\u043e\u0436\u0434\u0435\u043d\u0438\u044f \u043b\u0438\u0448\u043d\u0438\u0445 \u0443\u0440\u043e\u0432\u043d\u0435\u0439 \u043a\u043e\u0441\u0432\u0435\u043d\u043d\u043e\u0441\u0442\u0438 (<em>indirection<\/em>) \u043b\u0443\u0447\u0448\u0435 \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0442\u044c \u043f\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044e.<\/p>\n<p>\u041a \u043e\u0434\u043d\u043e\u043c\u0443 \u0438\u0437 \u0442\u0430\u043a\u0438\u0445 \u0442\u0438\u043f\u043e\u0432 \u043e\u0442\u043d\u043e\u0441\u0438\u0442\u0441\u044f <code>std::string_view<\/code>. \u041a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0442\u044c \u043f\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044e \u0432\u0441\u044e\u0434\u0443: \u0432 \u043d\u0430\u0448\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\u044b, \u0434\u0440\u0443\u0433\u0438\u0435 \u043c\u0435\u0442\u043e\u0434\u044b. \u041d\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0435 \u0432 \u0435\u0433\u043e \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0435 \u043c\u0435\u0442\u043e\u0434\u044b. \u0414\u043e \u043f\u0440\u0438\u043d\u044f\u0442\u0438\u044f <em>deducing this<\/em> \u0443 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u043e\u0432 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u043e\u0439 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 \u043d\u0435 \u0431\u044b\u043b\u043e \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u0432 \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u0440\u0430\u0437\u044b\u043c\u0435\u043d\u043e\u0432\u0430\u043d\u0438\u0439 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f <code>this<\/code> \u0432 \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0445 \u043c\u0435\u0442\u043e\u0434\u0430\u0445 <code>std::string_view<\/code>.<\/p>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0436\u0435 \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043f\u0435\u0440\u0435\u043f\u0438\u0441\u0430\u0442\u044c \u0432\u0441\u0435 \u0435\u0433\u043e \u043c\u0435\u0442\u043e\u0434\u044b, \u043d\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u044e\u0449\u0438\u0435 \u043c\u043e\u0434\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0439, \u0441 \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0435\u0439 \u044f\u0432\u043d\u043e\u0433\u043e \u043e\u0431\u044a\u0435\u043a\u0442\u043d\u043e\u0433\u043e \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430 \u043f\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044e, \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u0431\u0435\u0441\u043f\u043b\u0430\u0442\u043d\u043e \u044d\u0442\u0438\u043c \u043f\u043e\u043b\u0443\u0447\u0430\u044f \u0443\u043b\u0443\u0447\u0448\u0435\u043d\u0438\u0435 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438:<\/p>\n<pre><code class=\"cpp\">template &lt;class charT, class traits = char_traits&lt;charT>> class basic_string_view { private:     const_pointer data_;     size_type size_; public:     constexpr const_iterator begin(this basic_string_view self) {         return self.data_;     }      constexpr const_iterator end(this basic_string_view self) {         return self.data_ + self.size_;     }      constexpr size_t size(this basic_string_view self) {         return self.size_;     }      constexpr const_reference operator[](this basic_string_view self, size_type pos) {         return self.data_[pos];     } };<\/code><\/pre>\n<h2>\u0417\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/h2>\n<p>\u041d\u0430 \u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435, \u044d\u0442\u043e \u0434\u0430\u043b\u0435\u043a\u043e \u043d\u0435 \u0432\u0441\u0435, \u0447\u0442\u043e \u043c\u043e\u0436\u043d\u043e \u0441\u043a\u0430\u0437\u0430\u0442\u044c \u043f\u0440\u043e <em>deducing this<\/em>, \u043d\u043e \u0441\u0430\u043c\u043e\u0435 \u0433\u043b\u0430\u0432\u043d\u043e\u0435 \u0438 \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u0435. \u0426\u0435\u043b\u044c\u044e \u0434\u0430\u043d\u043d\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0438 \u043d\u0435 \u044f\u0432\u043b\u044f\u043b\u043e\u0441\u044c \u0443\u0433\u043b\u0443\u0431\u043b\u0435\u043d\u0438\u0435 \u0432 \u0434\u0435\u0442\u0430\u043b\u0438.<\/p>\n<p>\u0415\u0441\u043b\u0438 \u0432\u044b \u0445\u043e\u0442\u0438\u0442\u0435 \u043f\u043e\u0441\u0442\u0438\u0447\u044c \u0432\u0441\u0435 \u043d\u044e\u0430\u043d\u0441\u044b \u2014 \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u043e\u0431\u0440\u0430\u0442\u0438\u0442\u044c\u0441\u044f \u043a <a href=\"https:\/\/www.open-std.org\/jtc1\/sc22\/wg21\/docs\/papers\/2021\/p0847r6.html\" rel=\"noopener noreferrer nofollow\">\u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b\u044c\u043d\u043e\u043c\u0443 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0443<\/a>.<\/p>\n<p>\u0422\u0430\u043a\u0436\u0435 \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u044b \u0438 \u0443\u0432\u043b\u0435\u043a\u0430\u0442\u0435\u043b\u044c\u043d\u044b \u0441\u0442\u0430\u0442\u044c\u0438, \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u043d\u044b\u0435 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u043c\u0438 \u0438\u0437 \u0435\u0433\u043e \u0430\u0432\u0442\u043e\u0440\u043e\u0432: <a href=\"https:\/\/devblogs.microsoft.com\/cppblog\/cpp23-deducing-this\/\" rel=\"noopener noreferrer nofollow\"><em>\u00abC++23\u2019s Deducing this: what it is, why it is, how to use it\u00bb<\/em><\/a> (Sy Brand), <a href=\"https:\/\/brevzin.github.io\/about\/\" rel=\"noopener noreferrer nofollow\"><em>\u00abCopy-on-write with Deducing this\u00bb<\/em><\/a> (Barry Revzin).<\/p>\n<p>\u041b\u044e\u0431\u0438\u0442\u0435 \u043f\u043b\u044e\u0441\u044b \u0438 \u0431\u0443\u0434\u044c\u0442\u0435 \u0441\u0447\u0430\u0441\u0442\u043b\u0438\u0432\u044b.<\/p>\n<\/p>\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\/post\/722668\/\"> https:\/\/habr.com\/ru\/post\/722668\/<\/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-346826","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/346826","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=346826"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/346826\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=346826"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=346826"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=346826"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}