{"id":297840,"date":"2020-01-28T15:00:58","date_gmt":"2020-01-28T15:00:58","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=297840"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=297840","title":{"rendered":"\u041f\u043e\u043c\u043e\u0449\u044c \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440\u0443 \u0421++ \u0432 \u0440\u0430\u0437\u0440\u0435\u0448\u0435\u043d\u0438\u0438 \u043f\u0435\u0440\u0435\u0433\u0440\u0443\u0437\u043a\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u0439"},"content":{"rendered":"\n<div class=\"post__text post__text-html\" id=\"post-content-body\" data-io-article-url=\"https:\/\/habr.com\/ru\/post\/485854\/\">\u0412 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0441\u043b\u0443\u0447\u0430\u044f\u0445 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440\u044b \u0421++ \u043d\u0435 \u043c\u043e\u0433\u0443\u0442 \u0432\u044b\u0431\u0440\u0430\u0442\u044c \u043f\u043e\u0434\u0445\u043e\u0434\u044f\u0449\u0443\u044e \u043f\u0435\u0440\u0435\u0433\u0440\u0443\u0436\u0430\u0435\u043c\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0432 \u043e\u0447\u0435\u0432\u0438\u0434\u043d\u043e\u0439 \u0441 \u0447\u0435\u043b\u043e\u0432\u0435\u0447\u0435\u0441\u043a\u043e\u0439 \u0442\u043e\u0447\u043a\u0438 \u0437\u0440\u0435\u043d\u0438\u044f \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u0438 \u2014 \u0432\u043e\u0437\u043d\u0438\u043a\u0430\u0435\u0442 \u043e\u0448\u0438\u0431\u043a\u0430 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0446\u0438\u0438:<\/p>\n<pre><code class=\"plaintext\">void f(int i){} void f(string s){}  vector&lt;int&gt; int_c = { 1, 2, 3, 4, 5 }; vector&lt;string&gt; string_c = { &quot;Sic&quot; ,&quot;transit&quot; ,&quot;gloria&quot; ,&quot;mundi&quot; }; for_each(begin(int_c), end(int_c), f);\/\/error C2672: &quot;for_each&quot;: \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d\u0430 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0430\u044f \u043f\u0435\u0440\u0435\u0433\u0440\u0443\u0436\u0435\u043d\u043d\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f<\/code><\/pre>\n<p><a name=\"habracut\"><\/a><br \/>  \u041f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 \u2013 \u0432 \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0438\u0438 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432 \u0443 f \u0432 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0439 \u0441\u0442\u0440\u043e\u043a\u0435, \u043f\u043e \u043a\u043e\u0442\u043e\u0440\u044b\u043c \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 \u043c\u043e\u0433 \u0431\u044b \u043f\u0440\u043e\u0438\u0437\u0432\u0435\u0441\u0442\u0438 \u0440\u0430\u0437\u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u043f\u0435\u0440\u0435\u0433\u0440\u0443\u0437\u043a\u0438. \u0422\u043e, \u0447\u0442\u043e \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430\u043c\u0438 for_each \u043f\u0435\u0440\u0435\u0434\u0430\u044e\u0442\u0441\u044f \u0438\u0442\u0435\u0440\u0430\u0442\u043e\u0440\u044b \u0432\u043f\u043e\u043b\u043d\u0435 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u043e\u0433\u043e \u0442\u0438\u043f\u0430 \u2013 \u0434\u043b\u044f \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440\u0430 \u043d\u0435 \u0438\u043c\u0435\u0435\u0442 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f.<\/p>\n<p>  \u041c\u043e\u0436\u043d\u043e \u043f\u0440\u0435\u0434\u043b\u043e\u0436\u0438\u0442\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u00ab\u043b\u043e\u0431\u043e\u0432\u044b\u0445\u00bb \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u0432 \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b:<\/p>\n<p>  1) \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 static_cast&lt;&gt;() \u0434\u043b\u044f \u043f\u0440\u0438\u043d\u0443\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0433\u043e \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d\u0438\u044f \u043a \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044e \u043d\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u043d\u0443\u0436\u043d\u043e\u0433\u043e \u0442\u0438\u043f\u0430.<\/p>\n<p>  \u041f\u0440\u0438\u0432\u043e\u0434\u0438\u043c \u043a \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044e \u043d\u0430 void f(int i):<\/p>\n<pre><code class=\"plaintext\">std::for_each(begin(int_c), end(int_c), static_cast&lt;void (*)(int)&gt;(&amp;f)); <\/code><\/pre>\n<p>  \u041f\u0440\u0438\u0432\u043e\u0434\u0438\u043c \u043a \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044e \u043d\u0430 void f(string i):<\/p>\n<pre><code class=\"plaintext\">std::for_each(begin(string_c), end(string_c), static_cast&lt;void (*)(string)&gt;(&amp;f));<\/code><\/pre>\n<p>  2) \u041e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u0435\u0439 \u043d\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0438 \u0438\u0445 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435.<\/p>\n<p>  \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c void f(int i):<\/p>\n<pre><code class=\"plaintext\"> void (*fp_int)(int) = &f;   for_each(begin(int_c), end(int_c), fp_int);<\/code><\/pre>\n<p>  \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c void f(string i):<\/p>\n<pre><code class=\"plaintext\">void (*fp_string)(string) = &f; for_each(begin(string_c), end(string_c), fp_string);  <\/code><\/pre>\n<p>  3) \u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u044f\u0432\u043d\u044b\u0445 \u0441\u043f\u0435\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0439 \u0434\u043b\u044f \u0448\u0430\u0431\u043b\u043e\u043d\u043d\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 for_each:<\/p>\n<p>  \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c void f(int i):<\/p>\n<pre><code class=\"plaintext\">std::for_each&lt;vector&lt;int&gt;::const_iterator, void(*)(int) &gt;(int_c.begin(), int_c.end(), f);<\/code><\/pre>\n<p>  \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c void f(string i):<\/p>\n<pre><code class=\"plaintext\">std::for_each&lt;vector&lt;string&gt;::const_iterator, void(*)(string)  &gt;(string_c.begin(), string_c.end(), f);<\/code><\/pre>\n<p>  \u0438\u043b\u0438 \u0431\u043e\u043b\u0435\u0435 \u043a\u043e\u043c\u043f\u0430\u043a\u0442\u043d\u043e:<\/p>\n<pre><code class=\"plaintext\">std::for_each&lt;decltype(int_c.begin()), void(*)(int) &gt;(int_c.begin(), int_c.end(), f); std::for_each&lt;decltype(string_c.begin()), void(*)(string)  &gt;(string_c.begin(), string_c.end(), f);<\/code><\/pre>\n<p>  \u041f\u0440\u0435\u0434\u043b\u043e\u0436\u0435\u043d\u043d\u044b\u0435 \u0432\u044b\u0448\u0435 \u0441\u043f\u043e\u0441\u043e\u0431\u044b \u043d\u0435 \u0443\u043d\u0438\u0432\u0435\u0440\u0441\u0430\u043b\u044c\u043d\u044b \u0438 \u043f\u0440\u0438\u0432\u043e\u0434\u044f\u0442 \u043a \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u0438 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u043d\u0430 \u0432\u044b\u0437\u044b\u0432\u0430\u044e\u0449\u0435\u0439 \u0441\u0442\u043e\u0440\u043e\u043d\u0435 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0433\u043e \u043a\u043e\u0434\u0430. \u041b\u0443\u0447\u0448\u0435\u0439 \u0438\u0434\u0435\u0435\u0439 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u043d\u0430\u0434 \u0444\u0443\u043d\u043a\u0446\u0438\u0435\u0439 f \u0442\u043e\u043d\u043a\u043e\u0439 \u043e\u0431\u043e\u043b\u043e\u0447\u043a\u0438, \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u044e\u0449\u0435\u0439 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b \u0444\u0443\u043d\u043a\u0446\u0438\u0438 f \u0438 \u043f\u0435\u0440\u0435\u0434\u0430\u044e\u0449\u0435\u0439 \u0438\u0445 \u0432 \u043d\u0435\u0435 \u0431\u0435\u0437 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0439. <\/p>\n<p>  \u041d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0442\u0430\u043a\u0436\u0435 \u0443\u0447\u0438\u0442\u044b\u0432\u0430\u0442\u044c \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438:<\/p>\n<ul>\n<li>\u0441\u0443\u0449\u0435\u0441\u0442\u0432\u043e\u0432\u0430\u043d\u0438\u044f \u043d\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u043e\u0433\u043e \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0430 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432 \u0443 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 f,<\/li>\n<li>\u043d\u0430\u043b\u0438\u0447\u0438\u044f \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c\u043e\u0433\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u0438,<\/li>\n<li>\u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0438 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432 \u043f\u043e \u0441\u0441\u044b\u043b\u043a\u0435,<\/li>\n<li>\u0441\u0443\u0449\u0435\u0441\u0442\u0432\u043e\u0432\u0430\u043d\u0438\u044f cv-\u043a\u0432\u0430\u043b\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440\u044b \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432,<\/li>\n<li>\u0432\u043e\u0437\u043d\u0438\u043a\u043d\u043e\u0432\u0435\u043d\u0438\u044f \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0439 \u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 f.<\/li>\n<\/ul>\n<p>  \u041f\u043e\u044d\u0442\u0430\u043f\u043d\u043e \u0440\u0430\u0437\u0431\u0435\u0440\u0435\u043c \u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u0435 \u0442\u0430\u043a\u043e\u0439 \u043e\u0431\u043e\u043b\u043e\u0447\u043a\u0438. \u041b\u044f\u043c\u0431\u0434\u0430-\u0444\u0443\u043d\u043a\u0446\u0438\u044f \u2013 \u0445\u043e\u0440\u043e\u0448\u0430\u044f \u043e\u0441\u043d\u043e\u0432\u0430. \u041f\u0440\u0438 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0435 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440\u043e\u043c \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u0430 \u0421++11 \u044d\u0442\u043e \u043c\u043e\u0436\u043d\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0442\u0430\u043a:<\/p>\n<pre><code class=\"plaintext\">for_each(begin(int_c), end(int_c), [](int a) { return f(a); }); for_each(begin(string_c), end(string_c), [](string a) { return f(a); });<\/code><\/pre>\n<p>  \u0438\u043b\u0438 \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c decltype():<\/p>\n<pre><code class=\"plaintext\">for_each(begin(int_c), end(int_c), [](decltype(*int_c.begin()) a) { return f(a); }); for_each(begin(string_c), end(string_c), [](decltype(*string_c.begin()) a) { return f(a); });<\/code><\/pre>\n<p>  \u041f\u0440\u0438 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0435 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440\u043e\u043c \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u0430 \u0421++14 \u0438\u043c\u0435\u0435\u043c \u0434\u043b\u044f \u043f\u043e\u043b\u0438\u043c\u043e\u0440\u0444\u043d\u043e\u0439 \u043b\u044f\u043c\u0431\u0434\u044b \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435: <\/p>\n<pre><code class=\"plaintext\">auto l = [](auto a) { return f(a); };  for_each(begin(int_c), end(int_c), l); for_each(begin(string_c), end(string_c), l); <\/code><\/pre>\n<p>  \u0418\u0442\u0430\u043a, \u043c\u044b \u043f\u0435\u0440\u0435\u0434\u0430\u0435\u043c \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u043c \u0432 \u043b\u044f\u043c\u0431\u0434\u0443 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 f, \u0438 \u043f\u043e\u0442\u043e\u043c \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u043c f \u0441 \u044d\u0442\u0438\u043c \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u043c. \u041d\u043e \u0442\u0443\u0442 \u0435\u0441\u0442\u044c \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0430: \u043c\u044b \u043d\u0435 \u0437\u043d\u0430\u0435\u043c \u0437\u0430\u0440\u0430\u043d\u0435\u0435 \u2013 \u044f\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b \u0444\u0443\u043d\u043a\u0446\u0438\u0438 f \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f\u043c\u0438 \u0438\u043b\u0438 \u0441\u0441\u044b\u043b\u043a\u0430\u043c\u0438? \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0438\u0434\u0435\u0430\u043b\u044c\u043d\u0443\u044e \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0443.<\/p>\n<pre><code class=\"plaintext\">auto l = [](auto&amp;&amp; a) { return f(std::forward&lt;decltype(a)&gt;(a)); }; for_each(begin(int_c), end(int_c), l); for_each(begin(string_c), end(string_c), l);<\/code><\/pre>\n<p>  \u041f\u043e \u0442\u0435\u043c\u0435 \u0438\u0434\u0435\u0430\u043b\u044c\u043d\u043e\u0439 \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0438 (perfect forwarding) \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u043e\u0447\u0438\u0442\u0430\u0442\u044c \u0443 \u041c\u0435\u0439\u0435\u0440\u0441\u0430 [1]. \u0411\u043e\u043b\u0435\u0435 \u043f\u043e\u043d\u044f\u0442\u043d\u044b\u0439 (\u0434\u043b\u044f \u043c\u0435\u043d\u044f \u0432\u043e \u0432\u0441\u044f\u043a\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435) \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u0438\u0437\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0442\u043e\u0433\u043e \u0436\u0435 \u043c\u0430\u0442\u0435\u0440\u0438\u0430\u043b\u0430 \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u0439\u0442\u0438 \u0432 \u0441\u0442\u0430\u0442\u044c\u0435[2], \u043f\u0435\u0440\u0435\u0432\u043e\u0434 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u0435\u0441\u0442\u044c \u043d\u0430 \u0425\u0430\u0431\u0440\u0435 [3].<\/p>\n<p>  \u0414\u043b\u044f \u043d\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u043e\u0433\u043e \u0447\u0438\u0441\u043b\u0430 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u043c \u0432\u0430\u0440\u0438\u0430\u0442\u0438\u0432\u043d\u044b\u043c\u0438 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b \u043b\u044f\u043c\u0431\u0434\u044b \u0438 \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u043c std::forward \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0435\u043c\u043e\u0433\u043e \u0432 f \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430.<\/p>\n<pre><code class=\"plaintext\"> auto l = [](auto&amp;&amp;... a) { return f(std::forward&lt;decltype(a)&gt;(a)...); };<\/code><\/pre>\n<p>  \u0414\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0441\u043f\u0435\u0446\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440 \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0446\u0438\u0438 noexcept[4] \u0438 \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440 noexcept[5] \u0434\u043b\u044f \u0443\u043a\u0430\u0437\u0430\u043d\u0438\u044f \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440\u0443 \u2013 \u0431\u0443\u0434\u0435\u0442 \u043b\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u044f f \u0432\u044b\u0431\u0440\u0430\u0441\u044b\u0432\u0430\u0442\u044c \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f[6]. <\/p>\n<pre><code class=\"plaintext\">  auto l = [](auto&amp;&amp;... a) \\         noexcept(noexcept(f(std::forward&lt;decltype(a)&gt;(a)...)))\\     { return f(std::forward&lt;decltype(a)&gt;(a)...); };<\/code><\/pre>\n<p>  \u0414\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0432\u044b\u0432\u043e\u0434 \u0442\u0438\u043f\u0430 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c\u043e\u0433\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0434\u043b\u044f \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u0438\u0440\u0443\u0435\u043c\u043e\u0439 \u043b\u044f\u043c\u0431\u0434\u044b.<\/p>\n<pre><code class=\"plaintext\">   auto l = [](auto&amp;&amp;... a) \\         noexcept(noexcept(f(std::forward&lt;decltype(a)&gt;(a)...)))\\         -&gt; decltype(f(std::forward&lt;decltype(a)&gt;(a)...))\\     { return f(std::forward&lt;decltype(a)&gt;(a)...); };<\/code><\/pre>\n<p>  \u0415\u0441\u043b\u0438 \u0440\u0430\u0437\u0434\u0440\u0430\u0436\u0430\u0435\u0442 \u0442\u0440\u0435\u0445\u043a\u0440\u0430\u0442\u043d\u043e\u0435 \u043f\u043e\u0432\u0442\u043e\u0440\u0435\u043d\u0438\u0435 \u043a\u043e\u0434\u0430 \u0438 \u043d\u0435 \u0433\u043d\u0443\u0448\u0430\u0435\u043c\u0441\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043c\u0430\u043a\u0440\u043e\u0441\u044b \u2013 \u043c\u043e\u0436\u043d\u043e \u0443\u043c\u0435\u043d\u044c\u0448\u0438\u0442\u044c \u0440\u0430\u0437\u043c\u0435\u0440 \u043d\u0430 \u0432\u044b\u0437\u044b\u0432\u0430\u044e\u0449\u0435\u0439 \u0441\u0442\u043e\u0440\u043e\u043d\u0435.<\/p>\n<pre><code class=\"plaintext\">#define LIFT(foo) \\ [](auto&amp;&amp;... x) \\ noexcept(noexcept(foo(std::forward&lt;decltype(x)&gt;(x)...))) \\ -&gt; decltype(foo(std::forward&lt;decltype(x)&gt;(x)...)) \\ { return foo(std::forward&lt;decltype(x)&gt;(x)...); } <\/code><\/pre>\n<p>  \u0412 \u0438\u0442\u043e\u0433\u0435 \u0438\u043c\u0435\u0435\u043c \u043e\u0431\u043e\u043b\u043e\u0447\u043a\u0443, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043f\u043e\u043c\u043e\u0433\u0430\u0435\u0442 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440\u0443 \u0440\u0430\u0437\u0440\u0435\u0448\u0438\u0442\u044c \u043f\u0435\u0440\u0435\u0433\u0440\u0443\u0436\u0430\u0435\u043c\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e.<\/p>\n<pre><code class=\"plaintext\">for_each(begin(int_c), end(int_c), LIFT(f)); for_each(begin(string_c), end(string_c), LIFT(f));<\/code><\/pre>\n<p>  [1] S.Meyers \u201cEffective modern C++\u201d Item 24: Distinguish universal references from rvalue references.<br \/>  [2] <a href=\"https:\/\/eli.thegreenplace.net\/2014\/perfect-forwarding-and-universal-references-in-c\">eli.thegreenplace.net\/2014\/perfect-forwarding-and-universal-references-in-c<\/a><br \/>  [3] <a href=\"https:\/\/habr.com\/ru\/post\/242639\/\">habr.com\/ru\/post\/242639<\/a><br \/>  [4] <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/language\/noexcept_spec\">en.cppreference.com\/w\/cpp\/language\/noexcept_spec<\/a><br \/>  [5] <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/language\/noexcept\">en.cppreference.com\/w\/cpp\/language\/noexcept<\/a><br \/>  [6] <a href=\"https:\/\/habr.com\/ru\/post\/164221\/\">habr.com\/ru\/post\/164221<\/a><br \/>  [7] <a href=\"https:\/\/www.fluentcpp.com\/2017\/08\/01\/overloaded-functions-stl\/\">www.fluentcpp.com\/2017\/08\/01\/overloaded-functions-stl<\/a><br \/>  [8] <a href=\"https:\/\/www.youtube.com\/watch?v=I3T4lePH-yA\">www.youtube.com\/watch?v=I3T4lePH-yA<\/a><\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/485854\/\"> https:\/\/habr.com\/ru\/post\/485854\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"\n<div class=\"post__text post__text-html\" id=\"post-content-body\" data-io-article-url=\"https:\/\/habr.com\/ru\/post\/485854\/\">\u0412 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0441\u043b\u0443\u0447\u0430\u044f\u0445 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440\u044b \u0421++ \u043d\u0435 \u043c\u043e\u0433\u0443\u0442 \u0432\u044b\u0431\u0440\u0430\u0442\u044c \u043f\u043e\u0434\u0445\u043e\u0434\u044f\u0449\u0443\u044e \u043f\u0435\u0440\u0435\u0433\u0440\u0443\u0436\u0430\u0435\u043c\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0432 \u043e\u0447\u0435\u0432\u0438\u0434\u043d\u043e\u0439 \u0441 \u0447\u0435\u043b\u043e\u0432\u0435\u0447\u0435\u0441\u043a\u043e\u0439 \u0442\u043e\u0447\u043a\u0438 \u0437\u0440\u0435\u043d\u0438\u044f \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u0438 \u2014 \u0432\u043e\u0437\u043d\u0438\u043a\u0430\u0435\u0442 \u043e\u0448\u0438\u0431\u043a\u0430 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0446\u0438\u0438:<\/p>\n<pre><code class=\"plaintext\">void f(int i){} void f(string s){}  vector&lt;int&gt; int_c = { 1, 2, 3, 4, 5 }; vector&lt;string&gt; string_c = { &quot;Sic&quot; ,&quot;transit&quot; ,&quot;gloria&quot; ,&quot;mundi&quot; }; for_each(begin(int_c), end(int_c), f);\/\/error C2672: &quot;for_each&quot;: \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d\u0430 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0430\u044f \u043f\u0435\u0440\u0435\u0433\u0440\u0443\u0436\u0435\u043d\u043d\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f<\/code><\/pre>\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-297840","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/297840","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=297840"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/297840\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=297840"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=297840"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=297840"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}