{"id":338446,"date":"2022-09-16T15:00:43","date_gmt":"2022-09-16T15:00:43","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=338446"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=338446","title":{"rendered":"<span>\u0423\u043f\u0440\u043e\u0449\u0430\u0435\u043c \u043a\u043e\u0434 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e if constexpr \u0438 \u043a\u043e\u043d\u0446\u0435\u043f\u0442\u043e\u0432 C++17\/C++20<\/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<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/bda\/7ac\/849\/bda7ac84941d0128803179875d606ec0.png\" width=\"780\" height=\"439\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/bda\/7ac\/849\/bda7ac84941d0128803179875d606ec0.png\"\/><figcaption><\/figcaption><\/figure>\n<p>\u0414\u043e C++17 \u0443 \u043d\u0430\u0441 \u0431\u044b\u043b\u043e \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u043d\u0435\u044d\u043b\u0435\u0433\u0430\u043d\u0442\u043d\u044b\u0445 \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u0432 \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c <code>static if<\/code> (if, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0432\u043e \u0432\u0440\u0435\u043c\u044f \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0446\u0438\u0438). \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0443\u044e \u0434\u0438\u0441\u043f\u0435\u0442\u0447\u0435\u0440\u0438\u0437\u0430\u0446\u0438\u044e \u0438\u043b\u0438 SFINAE. \u041a \u0441\u0447\u0430\u0441\u0442\u044c\u044e, \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u044f \u0438\u0437\u043c\u0435\u043d\u0438\u043b\u0430\u0441\u044c \u043a \u043b\u0443\u0447\u0448\u0435\u043c\u0443, \u0432\u0435\u0434\u044c \u0442\u0435\u043f\u0435\u0440\u044c \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e <code>if constexpr<\/code>\u00a0 \u0438 \u043a\u043e\u043d\u0446\u0435\u043f\u0442\u0430\u043c\u0438 C++20!<\/p>\n<p>\u041d\u0443 \u0447\u0442\u043e \u0436, \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u0440\u0430\u0437\u0431\u0435\u0440\u0435\u043c\u0441\u044f, \u043a\u0430\u043a \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u044d\u0442\u043e \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0437\u0430\u043c\u0435\u043d\u044b <code>std::enable_if<\/code> \u043a\u043e\u0434\u0430!<\/p>\n<ul>\n<li>\n<p><em>\u041e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u0435 \u043e\u0442 \u0430\u043f\u0440\u0435\u043b\u044f 2021 \u0433.:<\/em> \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f, \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u044b\u0435 \u0441 C++20 \u2014 \u043a\u043e\u043d\u0446\u0435\u043f\u0442\u044b.<\/p>\n<\/li>\n<li>\n<p><em>\u041e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u0435 \u043e\u0442 \u0430\u0432\u0433\u0443\u0441\u0442\u0430 2022 \u0433.:<\/em> \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u043f\u0440\u0438\u043c\u0435\u0440\u044b <code>if constexpr<\/code> (\u0447\u0435\u0442\u0432\u0435\u0440\u0442\u044b\u0439).<\/p>\n<\/li>\n<\/ul>\n<h3>\u0412\u0432\u0435\u0434\u0435\u043d\u0438\u0435 \u00a0<\/h3>\n<p>If \u0432\u043e \u0432\u0440\u0435\u043c\u044f \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0446\u0438\u0438 \u0432 \u0444\u043e\u0440\u043c\u0435 <code>if constexpr<\/code> \u2014 \u044d\u0442\u043e \u0437\u0430\u043c\u0435\u0447\u0430\u0442\u0435\u043b\u044c\u043d\u0430\u044f \u0444\u0438\u0447\u0430, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0431\u044b\u043b\u0430 \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0430 \u0432 C++17. \u042d\u0442\u043e\u0442 \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b \u043c\u043e\u0436\u0435\u0442 \u043f\u043e\u043c\u043e\u0447\u044c \u043d\u0430\u043c \u0437\u043d\u0430\u0447\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0443\u043b\u0443\u0447\u0448\u0438\u0442\u044c \u0447\u0438\u0442\u0430\u0435\u043c\u043e\u0441\u0442\u044c \u043a\u043e\u0434\u0430 \u0441 \u0431\u043e\u043b\u044c\u0448\u0438\u043c\u0438 \u043d\u0430\u0433\u0440\u043e\u043c\u043e\u0436\u0434\u0435\u043d\u0438\u044f\u043c\u0438 \u0448\u0430\u0431\u043b\u043e\u043d\u043e\u0432.<\/p>\n<p>\u041a\u0440\u043e\u043c\u0435 \u0442\u043e\u0433\u043e, C++20 \u043f\u0440\u0438\u043d\u0435\u0441 \u043d\u0430\u043c \u043a\u043e\u043d\u0446\u0435\u043f\u0442\u044b (\u0441oncepts)! \u042d\u0442\u043e \u0435\u0449\u0435 \u043e\u0434\u0438\u043d \u0448\u0430\u0433 \u043d\u0430 \u043f\u0443\u0442\u0438 \u043a \u0434\u043e\u0441\u0442\u0438\u0436\u0435\u043d\u0438\u044e \u043f\u043e\u0447\u0442\u0438 \u201c\u043e\u0440\u0433\u0430\u043d\u0438\u0447\u043d\u043e\u0433\u043e\u201d \u043a\u043e\u0434\u0430 \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0446\u0438\u0438.<\/p>\n<p>\u041d\u0430 \u044d\u0442\u0443 \u0441\u0442\u0430\u0442\u044c\u044e \u043c\u0435\u043d\u044f \u0432\u0434\u043e\u0445\u043d\u043e\u0432\u0438\u043b \u043f\u043e\u0441\u0442 \u043d\u0430 <a href=\"https:\/\/meetingcpp.com\/blog\/items\/How-if-constexpr-simplifies-your-code-in-Cpp17.html\">@Meeting C++<\/a> \u0441 \u043e\u0447\u0435\u043d\u044c \u043f\u043e\u0445\u043e\u0436\u0438\u043c \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u0435\u043c. \u042f \u043d\u0430\u0448\u0435\u043b \u0435\u0449\u0435 \u0447\u0435\u0442\u044b\u0440\u0435 \u043f\u0440\u0438\u043c\u0435\u0440\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043d\u0430\u0433\u043b\u044f\u0434\u043d\u043e \u043f\u0440\u043e\u0434\u0435\u043c\u043e\u043d\u0441\u0442\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0432 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0438 \u044d\u0442\u043e\u0442 \u043d\u043e\u0432\u044b\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b:<\/p>\n<ul>\n<li>\n<p>\u0421\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u0435 \u0447\u0438\u0441\u0435\u043b<\/p>\n<\/li>\n<li>\n<p>(<strong>\u041d\u043e\u0432\u0438\u043d\u043a\u0430!<\/strong>) \u0412\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0435 \u0441\u0440\u0435\u0434\u043d\u0435\u0433\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0432 \u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440\u0435<\/p>\n<\/li>\n<li>\n<p>\u0424\u0430\u0431\u0440\u0438\u043a\u0438 \u0441 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u043c \u0447\u0438\u0441\u043b\u043e\u043c \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u0432<\/p>\n<\/li>\n<li>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440\u044b \u0440\u0435\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u043a\u043e\u0434\u0430 \u0438\u0437 \u043f\u0440\u043e\u0434\u0430\u043a\u0448\u0435\u043d\u0430\u00a0<\/p>\n<\/li>\n<\/ul>\n<p>\u041d\u043e \u0434\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430, \u0447\u0442\u043e\u0431\u044b \u0443 \u043d\u0430\u0441 \u0431\u044b\u043b\u043e \u0431\u043e\u043b\u044c\u0448\u0435 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430, \u044f \u0432\u0441\u0435-\u0442\u0430\u043a\u0438 \u0445\u043e\u0442\u0435\u043b \u0431\u044b \u043f\u0440\u043e\u0439\u0442\u0438\u0441\u044c \u043f\u043e \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u043c \u0431\u0430\u0437\u043e\u0432\u044b\u043c \u0441\u0432\u0435\u0434\u0435\u043d\u0438\u044f\u043c \u043e <code>enable_if<\/code>.<\/p>\n<h3>\u0414\u043b\u044f \u0447\u0435\u0433\u043e \u043c\u043e\u0436\u0435\u0442 \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u0442\u044c\u0441\u044f if \u0432\u043e \u0432\u0440\u0435\u043c\u044f \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0446\u0438\u0438?\u00a0 \u00a0\u00a0<\/h3>\n<p>\u041d\u0430\u0447\u043d\u0435\u043c \u0441 \u043f\u0440\u0438\u043c\u0435\u0440\u0430, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u043c\u044b \u043f\u044b\u0442\u0430\u0435\u043c\u0441\u044f \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u0432\u043e\u0434 \u0432 \u0441\u0442\u0440\u043e\u043a\u0443:<\/p>\n<pre><code class=\"cpp\">#include &lt;string> #include &lt;iostream>  template &lt;typename T> std::string str(T t) {     return std::to_string(t); }  std::string str(const std::string&amp; s) {     return s; }  std::string str(const char* s) {     return s; }  std::string str(bool b) {     return b ? \"true\" : \"false\"; }  int main() {     std::cout &lt;&lt; str(\"hello\") &lt;&lt; '\\n';     std::cout &lt;&lt; str(std::string{\"hi!\"}) &lt;&lt; '\\n';     std::cout &lt;&lt; str(42) &lt;&lt; '\\n';     std::cout &lt;&lt; str(42.2) &lt;&lt; '\\n';     std::cout &lt;&lt; str(true) &lt;&lt; '\\n'; }<\/code><\/pre>\n<p>\u0412\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u044d\u0442\u043e\u0442 \u043a\u043e\u0434 \u0432 <a href=\"https:\/\/godbolt.org\/z\/Y9q34e4ab\">Compiler Explorer<\/a>.<\/p>\n<p>\u0412 \u043a\u043e\u0434\u0435, \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d\u043d\u043e\u043c \u0432\u044b\u0448\u0435, \u043c\u044b \u0432\u0438\u0434\u0438\u043c \u0442\u0440\u0438 \u043f\u0435\u0440\u0435\u0433\u0440\u0443\u0437\u043a\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u0439 \u0434\u043b\u044f \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u044b\u0445 \u0442\u0438\u043f\u043e\u0432 \u0438 \u043e\u0434\u0438\u043d \u0448\u0430\u0431\u043b\u043e\u043d \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0434\u043b\u044f \u0432\u0441\u0435\u0445 \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0445 \u0442\u0438\u043f\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0434\u043e\u043b\u0436\u043d\u044b \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0442\u044c <code>to_string()<\/code>. \u041f\u043e\u0445\u043e\u0436\u0435, \u0447\u0442\u043e \u044d\u0442\u043e\u0442 \u043a\u043e\u0434 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442. \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u0432\u0441\u0435 \u044d\u0442\u043e \u0432 \u043e\u0434\u043d\u0443 \u0444\u0443\u043d\u043a\u0446\u0438\u044e.<\/p>\n<p>\u0421\u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043b\u0438 \u0437\u0434\u0435\u0441\u044c \u201c\u0437\u0430\u0443\u0440\u044f\u0434\u043d\u044b\u0439\u201d <code>if<\/code>?<\/p>\n<p>\u0412\u043e\u0442 \u0442\u0435\u0441\u0442\u043e\u0432\u044b\u0439 \u043a\u043e\u0434:<\/p>\n<pre><code class=\"cpp\">template &lt;typename T> std::string str(T t) {     if (std::is_convertible_v&lt;T, std::string>)         return t;     else if (std::is_same_v&lt;T, bool>)         return t ? \"true\" : \"false\";     else         return std::to_string(t); }<\/code><\/pre>\n<p>\u0417\u0432\u0443\u0447\u0438\u0442 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u043f\u0440\u043e\u0441\u0442\u043e\u2026 \u043d\u043e \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u0441\u043a\u043e\u043c\u043f\u0438\u043b\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u044d\u0442\u043e\u0442 \u043a\u043e\u0434:<\/p>\n<pre><code class=\"cpp\">\/\/ \u043a\u043e\u0434, \u0432\u044b\u0437\u044b\u0432\u0430\u044e\u0449\u0438\u0439 \u043d\u0430\u0448\u0443 \u0444\u0443\u043d\u043a\u0446\u0438\u044e auto t = str(\"10\"s);<\/code><\/pre>\n<p>\u0412 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435 \u043c\u044b \u0434\u043e\u043b\u0436\u043d\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0447\u0442\u043e-\u0442\u043e \u0432\u0440\u043e\u0434\u0435 \u044d\u0442\u043e\u0433\u043e:<\/p>\n<pre><code class=\"cpp\">In instantiation of 'std::__cxx11::string str(T) [with T =  std::__cxx11::basic_string&lt;char>; std::__cxx11::string =  std::__cxx11::basic_string&lt;char>]': required from here error: no matching function for call to  'to_string(std::__cxx11::basic_string&lt;char>&amp;)'     return std::to_string(t);<\/code><\/pre>\n<p><code>is_convertible<\/code>\u00a0 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 <code>true<\/code>\u00a0 \u0434\u043b\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u043e\u0433\u043e \u043d\u0430\u043c\u0438 \u0442\u0438\u043f\u0430 (<code>std::string<\/code>), \u0442\u0430\u043a \u0447\u0442\u043e \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043f\u0440\u043e\u0441\u0442\u043e \u0432\u0435\u0440\u043d\u0443\u0442\u044c <code>t<\/code>\u00a0 \u0431\u0435\u0437 \u043a\u0430\u043a\u043e\u0433\u043e-\u043b\u0438\u0431\u043e \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u044f\u2026 \u0442\u0430\u043a \u0447\u0442\u043e \u0436\u0435 \u043d\u0435 \u0442\u0430\u043a?<\/p>\n<p>\u0412\u043e\u0442 \u0432 \u0447\u0435\u043c \u0434\u0435\u043b\u043e:<\/p>\n<p>\u041a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 \u0441\u043a\u043e\u043c\u043f\u0438\u043b\u0438\u0440\u043e\u0432\u0430\u043b \u0432\u0441\u0435 \u0432\u0435\u0442\u043a\u0438 \u0438 \u043d\u0430\u0448\u0435\u043b \u043e\u0448\u0438\u0431\u043a\u0443 \u0432 <code>else<\/code>. \u041e\u043d \u043d\u0435 \u043c\u043e\u0436\u0435\u0442 \u043e\u0442\u0431\u0440\u043e\u0441\u0438\u0442\u044c \u201c\u043d\u0435\u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u044b\u0439\u201d \u043a\u043e\u0434 \u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0447\u0430\u0441\u0442\u043d\u043e\u0433\u043e \u0441\u043b\u0443\u0447\u0430\u044f \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u0438\u0437\u0430\u0446\u0438\u0438 \u0448\u0430\u0431\u043b\u043e\u043d\u0430.<\/p>\n<p>\u0412\u043e\u0442 \u0437\u0430\u0447\u0435\u043c \u043d\u0430\u043c \u043d\u0443\u0436\u0435\u043d \u0441\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0439 <code>if<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u201c\u043e\u0442\u0431\u0440\u043e\u0441\u0438\u0442\u201d \u043d\u0435\u043d\u0443\u0436\u043d\u044b\u0439 \u043a\u043e\u0434 \u0438 \u0441\u043a\u043e\u043c\u043f\u0438\u043b\u0438\u0440\u0443\u0435\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u0431\u043b\u043e\u043a, \u0432 \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u0435\u0434\u0435\u0442 \u0432\u0435\u0442\u0432\u043b\u0435\u043d\u0438\u0435. \u0418\u043d\u044b\u043c\u0438 \u0441\u043b\u043e\u0432\u0430\u043c\u0438, \u043c\u044b \u043f\u043e \u043f\u0440\u0435\u0436\u043d\u0435\u043c\u0443 \u0431\u0443\u0434\u0435\u043c \u0434\u0435\u043b\u0430\u0442\u044c \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0443 \u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0441\u0430 \u0434\u043b\u044f \u0432\u0441\u0435\u0433\u043e \u043a\u043e\u0434\u0430, \u043d\u043e \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0447\u0430\u0441\u0442\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043d\u0435 \u0431\u0443\u0434\u0443\u0442 \u0441\u043e\u0437\u0434\u0430\u043d\u044b.<\/p>\n<h3>std::enable_if \u00a0\u00a0<\/h3>\n<p>\u041e\u0434\u0438\u043d \u0438\u0437 \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u043c \u043c\u043e\u0436\u043d\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0439 if \u0432 C++11\/14 \u2014 \u044d\u0442\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c <code>enable_if<\/code>.<\/p>\n<p><code>enable_if<\/code> (\u0438 <code>enable_if_v<\/code> C++14). \u0423 \u043d\u0435\u0433\u043e \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u0441\u0442\u0440\u0430\u043d\u043d\u044b\u0439 \u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0441:<\/p>\n<pre><code class=\"cpp\">template&lt; bool B, class T = void >   struct enable_if;<\/code><\/pre>\n<p><code>enable_if<\/code> \u0432\u044b\u0432\u043e\u0434\u0438\u0442 \u0442\u0438\u043f T, \u0435\u0441\u043b\u0438 \u0432\u0445\u043e\u0434\u043d\u043e\u0435 \u0443\u0441\u043b\u043e\u0432\u0438\u0435 B \u0438\u0441\u0442\u0438\u043d\u043d\u043e. \u0412 \u043f\u0440\u043e\u0442\u0438\u0432\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u044d\u0442\u043e <a href=\"https:\/\/www.cppstories.com\/2016\/02\/notes-on-c-sfinae.html\">SFINAE<\/a>, \u0438 \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u0430\u044f \u043f\u0435\u0440\u0435\u0433\u0440\u0443\u0437\u043a\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0443\u0434\u0430\u043b\u044f\u0435\u0442\u0441\u044f \u0438\u0437 \u043d\u0430\u0431\u043e\u0440\u0430 \u043f\u0435\u0440\u0435\u0433\u0440\u0443\u0437\u043e\u043a. \u042d\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e \u0432 \u0441\u043b\u0443\u0447\u0430\u0435 false \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 \u201c\u043e\u0442\u0431\u0440\u0430\u0441\u044b\u0432\u0430\u0435\u0442\u201d \u043a\u043e\u0434 \u2014 \u044d\u0442\u043e \u043a\u0430\u043a \u0440\u0430\u0437 \u0442\u043e, \u0447\u0442\u043e \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e.<\/p>\n<p>\u041c\u044b \u043c\u043e\u0436\u0435\u043c \u043f\u0435\u0440\u0435\u043f\u0438\u0441\u0430\u0442\u044c \u043d\u0430\u0448 \u043f\u0440\u0438\u043c\u0435\u0440 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:<\/p>\n<pre><code class=\"cpp\">template &lt;typename T> enable_if_t&lt;is_convertible_v&lt;T, string>, string> strOld(T t) {     return t; }  template &lt;typename T> enable_if_t&lt;!is_convertible_v&lt;T, string>, string> strOld(T t) {     return to_string(t); } \/\/ \u043f\u0440\u0435\u0444\u0438\u043a\u0441 std:: \u0431\u044b\u043b \u043e\u043f\u0443\u0449\u0435\u043d<\/code><\/pre>\n<p>\u041d\u0435 \u0442\u0430\u043a \u043b\u0435\u0433\u043a\u043e\u2026 \u0432\u0435\u0440\u043d\u043e? \u041a\u0440\u043e\u043c\u0435 \u0442\u043e\u0433\u043e, \u044d\u0442\u0430 \u0432\u0435\u0440\u0441\u0438\u044f \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u043d\u0430\u043c\u043d\u043e\u0433\u043e \u0441\u043b\u043e\u0436\u043d\u0435\u0435, \u0447\u0435\u043c \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0438 \u043e\u0431\u044b\u0447\u043d\u0430\u044f \u043f\u0435\u0440\u0435\u0433\u0440\u0443\u0437\u043a\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u0439, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0431\u044b\u043b\u0438 \u0443 \u043d\u0430\u0441 \u0432 \u0441\u0430\u043c\u043e\u043c \u043d\u0430\u0447\u0430\u043b\u0435.<\/p>\n<p>\u0412\u043e\u0442 \u043f\u043e\u0447\u0435\u043c\u0443 \u043d\u0430\u043c \u043d\u0443\u0436\u0435\u043d <code>if constexpr<\/code> \u0438\u0437 C++17, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u043e\u0436\u0435\u0442 \u043f\u043e\u043c\u043e\u0447\u044c \u0432 \u0442\u0430\u043a\u0438\u0445 \u0441\u043b\u0443\u0447\u0430\u044f\u0445.<\/p>\n<p>\u041f\u043e\u0447\u0438\u0442\u0430\u0432 \u044d\u0442\u0443 \u0441\u0442\u0430\u0442\u044c\u044e, \u0432\u044b \u0441\u0430\u043c\u0438 \u0441\u043c\u043e\u0436\u0435\u0442\u0435 \u0431\u044b\u0441\u0442\u0440\u043e \u043f\u0435\u0440\u0435\u043f\u0438\u0441\u0430\u0442\u044c \u043d\u0430\u0448\u0443 \u0444\u0443\u043d\u043a\u0446\u0438\u044e <code>str<\/code> (\u0438\u043b\u0438 \u043d\u0430\u0439\u0442\u0438 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0432 \u043a\u043e\u043d\u0446\u0435).<\/p>\n<p>\u0427\u0442\u043e\u0431\u044b \u043b\u0443\u0447\u0448\u0435 \u0440\u0430\u0437\u043e\u0431\u0440\u0430\u0442\u044c\u0441\u044f \u0441 \u043d\u043e\u0432\u044b\u043c\u0438 \u0444\u0438\u0447\u0430\u043c\u0438, \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0431\u0430\u0437\u043e\u0432\u044b\u0445 \u043f\u0440\u0438\u043c\u0435\u0440\u043e\u0432:<\/p>\n<h3>\u041f\u0440\u0438\u043c\u0435\u0440 1 \u2014 \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u0435 \u0447\u0438\u0441\u0435\u043b\u00a0 \u00a0\u00a0<\/h3>\n<p>\u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u043d\u0430\u0447\u043d\u0435\u043c \u0441 \u0441\u0430\u043c\u043e\u0433\u043e \u043f\u0440\u043e\u0441\u0442\u043e\u0433\u043e \u043f\u0440\u0438\u043c\u0435\u0440\u0430: \u0444\u0443\u043d\u043a\u0446\u0438\u044f <code>close_enough<\/code>, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0441 \u0434\u0432\u0443\u043c\u044f \u0447\u0438\u0441\u043b\u0430\u043c\u0438. \u0415\u0441\u043b\u0438 \u0447\u0438\u0441\u043b\u0430 \u043d\u0435 \u044f\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u0447\u0438\u0441\u043b\u0430\u043c\u0438 \u0441 \u043f\u043b\u0430\u0432\u0430\u044e\u0449\u0435\u0439 \u0437\u0430\u043f\u044f\u0442\u043e\u0439 (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043a\u043e\u0433\u0434\u0430 \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0434\u0432\u0430 <code>int<\/code>), \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0441\u0440\u0430\u0432\u043d\u0438\u0442\u044c \u0438\u0445 \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e. \u0414\u043b\u044f \u0447\u0438\u0441\u0435\u043b \u0441 \u043f\u043b\u0430\u0432\u0430\u044e\u0449\u0435\u0439 \u0437\u0430\u043f\u044f\u0442\u043e\u0439 \u043b\u0443\u0447\u0448\u0435 \u0431\u0443\u0434\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u0443\u044e \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u043c\u0430\u043b\u0435\u043d\u044c\u043a\u0443\u044e \u0432\u0435\u043b\u0438\u0447\u0438\u043d\u0443, \u0441 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u0441\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u0442\u044c \u0438\u0445 \u0440\u0430\u0437\u043d\u0438\u0446\u0443 <code>abs &lt; epsilon<\/code>.<\/p>\n<p>\u042f \u043d\u0430\u0448\u0435\u043b \u044d\u0442\u043e\u0442 \u043a\u043e\u0434 \u0432 <a href=\"https:\/\/h-deb.clg.qc.ca\/WG21\/PracticalCpp\/Temperature.html\">Practical Modern C++ Teaser<\/a> \u2014 \u0444\u0430\u043d\u0442\u0430\u0441\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u043c \u043f\u043e\u0448\u0430\u0433\u043e\u0432\u043e\u043c \u0440\u0443\u043a\u043e\u0432\u043e\u0434\u0441\u0442\u0432\u0435 \u043f\u043e \u0441\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u043c \u0444\u0438\u0447\u0430\u043c C++, \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u043d\u043e\u043c \u041f\u0430\u0442\u0440\u0438\u0441\u043e\u043c \u0420\u043e\u0435\u043c (<a href=\"https:\/\/twitter.com\/PatriceRoy1\">Patrice Roy<\/a>). \u041e\u043d \u0431\u044b\u043b \u043e\u0447\u0435\u043d\u044c \u043b\u044e\u0431\u0435\u0437\u0435\u043d \u0438 \u043f\u043e\u0437\u0432\u043e\u043b\u0438\u043b \u043c\u043d\u0435 \u0432\u043a\u043b\u044e\u0447\u0438\u0442\u044c \u044d\u0442\u043e\u0442 \u043f\u0440\u0438\u043c\u0435\u0440 \u0432 \u0441\u0432\u043e\u044e \u0441\u0442\u0430\u0442\u044c\u044e.<\/p>\n<p>\u0412\u0435\u0440\u0441\u0438\u044f \u0421++11\/14:<\/p>\n<pre><code class=\"cpp\">template &lt;class T> constexpr T absolute(T arg) {    return arg &lt; 0 ? -arg : arg; }  template &lt;class T>  constexpr enable_if_t&lt;is_floating_point&lt;T>::value, bool>  close_enough(T a, T b) {    return absolute(a - b) &lt; static_cast&lt;T>(0.000001); } template &lt;class T> constexpr enable_if_t&lt;!is_floating_point&lt;T>::value, bool>  close_enough(T a, T b) {    return a == b; }<\/code><\/pre>\n<p>\u041a\u0430\u043a \u0432\u0438\u0434\u0438\u0442\u0435, \u0437\u0434\u0435\u0441\u044c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f <code>enable_if<\/code>. \u042d\u0442\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u043e\u0447\u0435\u043d\u044c \u043f\u043e\u0445\u043e\u0436\u0430 \u043d\u0430 \u043d\u0430\u0448\u0443 \u0444\u0443\u043d\u043a\u0446\u0438\u044e <code>str<\/code>. \u041a\u043e\u0434 \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u0442 \u0442\u0438\u043f \u0432\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u0447\u0438\u0441\u043b\u0430 \u2014 <code>is_floating_point<\/code>. \u0417\u0430\u0442\u0435\u043c \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 \u043c\u043e\u0436\u0435\u0442 \u0443\u0434\u0430\u043b\u0438\u0442\u044c \u043e\u0434\u043d\u0443 \u0438\u0437 \u043f\u0435\u0440\u0435\u0433\u0440\u0443\u0437\u043e\u043a \u0444\u0443\u043d\u043a\u0446\u0438\u0438.<\/p>\n<p>\u0410 \u0442\u0435\u043f\u0435\u0440\u044c \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043d\u0430 \u0432\u0435\u0440\u0441\u0438\u044e C++17:<\/p>\n<pre><code class=\"cpp\">template &lt;class T> constexpr T absolute(T arg) {    return arg &lt; 0 ? -arg : arg; }  template &lt;class T> constexpr auto precision_threshold = T(0.000001);  template &lt;class T> constexpr bool close_enough(T a, T b) {    if constexpr (is_floating_point_v&lt;T>) \/\/ &lt;&lt; !!       return absolute(a - b) &lt; precision_threshold&lt;T>;    else       return a == b; }<\/code><\/pre>\n<p>\u0412\u0430\u0443\u2026 \u0432\u0441\u0435 \u0437\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u043e \u0432 \u043e\u0434\u043d\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043a \u0442\u043e\u043c\u0443 \u0436\u0435 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u043f\u043e\u0447\u0442\u0438 \u043a\u0430\u043a \u043e\u0431\u044b\u0447\u043d\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f.<\/p>\n<p>\u0421 \u043f\u043e\u0447\u0442\u0438 \u201c\u0437\u0430\u0443\u0440\u044f\u0434\u043d\u044b\u043c\u201d <code>if<\/code> \ud83d\ude42<\/p>\n<p>\u0412 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u0438 <code>if constexpr constexpr<\/code> \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u0432\u044b\u0447\u0438\u0441\u043b\u044f\u0435\u0442\u0441\u044f \u0432\u043e \u0432\u0440\u0435\u043c\u044f \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0446\u0438\u0438, \u043f\u043e\u0441\u043b\u0435 \u0447\u0435\u0433\u043e \u043a\u043e\u0434 \u0432 \u043e\u0434\u043d\u043e\u0439 \u0438\u0437 \u0432\u0435\u0442\u0432\u0435\u0439 \u043e\u0442\u0431\u0440\u0430\u0441\u044b\u0432\u0430\u0435\u0442\u0441\u044f.<\/p>\n<p>\u041d\u043e \u0432\u0430\u0436\u043d\u043e \u043e\u0442\u043c\u0435\u0442\u0438\u0442\u044c, \u0447\u0442\u043e \u043e\u0442\u0431\u0440\u043e\u0448\u0435\u043d\u043d\u044b\u0439 \u043a\u043e\u0434 \u0432\u0441\u0435-\u0442\u0430\u043a\u0438 \u0434\u043e\u043b\u0436\u0435\u043d \u0438\u043c\u0435\u0442\u044c \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u044b\u0439 \u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0441. \u041a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442 \u0431\u0430\u0437\u043e\u0432\u0443\u044e \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0443 \u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0441\u0430, \u043d\u043e \u0437\u0430\u0442\u0435\u043c \u043f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u0442 \u044d\u0442\u0443 \u0447\u0430\u0441\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043d\u0430 \u044d\u0442\u0430\u043f\u0435 \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u0438\u0437\u0430\u0446\u0438\u0438 \u0448\u0430\u0431\u043b\u043e\u043d\u0430.<\/p>\n<p>\u0418\u043c\u0435\u043d\u043d\u043e \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u043a\u043e\u0434 \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u0442 \u043e\u0448\u0438\u0431\u043a\u0443 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440\u0430:<\/p>\n<pre><code class=\"cpp\">template &lt;class T> constexpr bool close_enough(T a, T b) {    if constexpr (is_floating_point_v&lt;T>)        return absolute(a - b) &lt; precision_threshold&lt;T>;    else       return aaaa == bxxxx; \/\/ \u043e\u0448\u0438\u0431\u043a\u0430 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440\u0430 - \u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0441! }  close_enough(10.04f, 20.f);<\/code><\/pre>\n<p>\u041a\u0441\u0442\u0430\u0442\u0438, \u0437\u0430\u043c\u0435\u0442\u0438\u043b\u0438 \u043b\u0438 \u0432\u044b \u043a\u0430\u043a\u0438\u0435-\u043d\u0438\u0431\u0443\u0434\u044c \u0434\u0440\u0443\u0433\u0438\u0435 \u0444\u0438\u0447\u0438 C++17, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0437\u0434\u0435\u0441\u044c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b\u0438\u0441\u044c?<\/p>\n<p>\u0412\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u044d\u0442\u043e\u0442 \u043a\u043e\u0434 \u0432 <a href=\"https:\/\/godbolt.org\/z\/KbaahbP1x\">@Compiler Explorer<\/a><\/p>\n<h4>\u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u043a\u043e\u043d\u0446\u0435\u043f\u0442\u043e\u0432 C++20 \u00a0\u00a0<\/h4>\n<p>\u041d\u043e \u043f\u043e\u0434\u043e\u0436\u0434\u0438\u0442\u0435\u2026 \u043d\u0430 \u0434\u0432\u043e\u0440\u0435 \u0443\u0436\u0435 2021 \u0433\u043e\u0434, \u0442\u0430\u043a \u043f\u043e\u0447\u0435\u043c\u0443 \u0431\u044b \u043d\u0430\u043c \u043d\u0435 \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u043a\u043e\u043d\u0446\u0435\u043f\u0442\u0430\u043c\u0438? \ud83d\ude42<\/p>\n<p>\u0414\u043e C++20 \u043c\u044b \u043c\u043e\u0433\u043b\u0438 \u0440\u0430\u0441\u0441\u043c\u0430\u0442\u0440\u0438\u0432\u0430\u0442\u044c \u0448\u0430\u0431\u043b\u043e\u043d\u043d\u044b\u0435 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b \u043a\u0430\u043a \u0447\u0442\u043e-\u0442\u043e \u0432\u0440\u043e\u0434\u0435 void* \u0432 \u043e\u0431\u044b\u0447\u043d\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438. \u0415\u0441\u043b\u0438 \u0432\u044b \u0445\u043e\u0442\u0435\u043b\u0438 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0438\u0442\u044c \u0442\u0430\u043a\u043e\u0439 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440, \u0432\u0430\u043c \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u043b\u043e\u0441\u044c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0435 \u043c\u0435\u0442\u043e\u0434\u044b, \u043e\u043f\u0438\u0441\u0430\u043d\u043d\u044b\u0435 \u0432 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435. \u041d\u043e \u0432\u043c\u0435\u0441\u0442\u0435 \u0441 \u043a\u043e\u043d\u0446\u0435\u043f\u0442\u0430\u043c\u0438 \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u0438 \u0435\u0441\u0442\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0439 \u0441\u043f\u043e\u0441\u043e\u0431 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0438\u0442\u044c \u044d\u0442\u0438 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b.<\/p>\n<p>\u0412\u0437\u0433\u043b\u044f\u043d\u0438\u0442\u0435 \u043d\u0430 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442 \u043a\u043e\u0434\u0430:\u00a0<\/p>\n<pre><code class=\"cpp\">template &lt;typename T> requires std::is_floating_point_v&lt;T> constexpr bool close_enough20(T a, T b) {    return absolute(a - b) &lt; precision_threshold&lt;T>; } constexpr bool close_enough20(auto a, auto b) {    return a == b; }<\/code><\/pre>\n<p>\u041a\u0430\u043a \u0432\u0438\u0434\u0438\u0442\u0435, \u0432\u0435\u0440\u0441\u0438\u044f C++20 \u0432\u0435\u0440\u043d\u0443\u043b\u0430\u0441\u044c \u043e\u0431\u0440\u0430\u0442\u043d\u043e \u043a \u0434\u0432\u0443\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u044f\u043c. \u041d\u043e \u0442\u0435\u043f\u0435\u0440\u044c \u043a\u043e\u0434 \u043d\u0430\u043c\u043d\u043e\u0433\u043e \u0447\u0438\u0442\u0430\u0431\u0435\u043b\u044c\u043d\u0435\u0435, \u0447\u0435\u043c \u0441 <code>enable_if<\/code>. \u0421 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043a\u043e\u043d\u0446\u0435\u043f\u0442\u043e\u0432 \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043b\u0435\u0433\u043a\u043e \u0432\u044b\u0440\u0430\u0437\u0438\u0442\u044c \u043d\u0430\u0448\u0438 \u0442\u0440\u0435\u0431\u043e\u0432\u0430\u043d\u0438\u044f \u043a \u0448\u0430\u0431\u043b\u043e\u043d\u043d\u044b\u043c \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430\u043c:<\/p>\n<pre><code class=\"cpp\">requires std::is_floating_point_v&lt;T><\/code><\/pre>\n<p><code>is_floating_point_v<\/code> \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u043e\u043c \u0442\u0438\u043f\u0430 (\u0438\u0437 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 <code>&lt;type_traits><\/code>), \u0430 \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440 <code>requires<\/code>, \u043a\u0430\u043a \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u0432\u0438\u0434\u0435\u0442\u044c, \u0432\u044b\u0447\u0438\u0441\u043b\u044f\u0435\u0442 \u0431\u0443\u043b\u0435\u0432\u044b \u043a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u043d\u044b\u0435 \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u044f.<\/p>\n<p>\u0412\u0442\u043e\u0440\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u043d\u043e\u0432\u044b\u0439 \u043e\u0431\u043e\u0431\u0449\u0435\u043d\u043d\u044b\u0439 \u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0441 \u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c <code>template&lt;><\/code> \u0438 \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c:<\/p>\n<pre><code class=\"cpp\">constexpr bool close_enough20(auto a, auto b) { }<\/code><\/pre>\n<p>\u0422\u0430\u043a\u043e\u0439 \u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0441 \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u0438 \u0441 <a href=\"https:\/\/www.cppstories.com\/2020\/08\/lambda-generic.html\/\">\u043e\u0431\u043e\u0431\u0449\u0435\u043d\u043d\u044b\u043c\u0438 (generic) \u043b\u044f\u043c\u0431\u0434\u0430-\u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u044f\u043c\u0438<\/a>. \u042d\u0442\u043e \u043d\u0435 \u043f\u0440\u044f\u043c\u0430\u044f \u0442\u0440\u0430\u043d\u0441\u043b\u044f\u0446\u0438\u044f \u043d\u0430\u0448\u0435\u0433\u043e C++11\/14 \u043a\u043e\u0434\u0430, \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u043e\u043d \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u0435\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439 \u0441\u0438\u0433\u043d\u0430\u0442\u0443\u0440\u0435:<\/p>\n<pre><code class=\"cpp\">template &lt;typename T, typename U> constexpr bool close_enough20(T a, U b) { }<\/code><\/pre>\n<p>\u0412\u0434\u043e\u0431\u0430\u0432\u043e\u043a, C++20 \u043f\u0440\u0435\u0434\u043b\u0430\u0433\u0430\u0435\u0442 \u043d\u0430\u043c \u043a\u0440\u0430\u0442\u043a\u0438\u0439 \u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0441 \u0434\u043b\u044f \u043a\u043e\u043d\u0446\u0435\u043f\u0442\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043e\u0441\u043d\u043e\u0432\u0430\u043d \u043d\u0430 auto \u0441 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f\u043c\u0438 (constrained auto):<\/p>\n<pre><code class=\"cpp\">constexpr bool close_enough20(std::floating_point auto a,                               std::floating_point auto b) {    return absolute(a - b) &lt; precision_threshold&lt;std::common_type_t&lt;decltype(a), decltype(b)>>; } constexpr bool close_enough20(std::integral auto a, std::integral auto b) {    return a == b; }<\/code><\/pre>\n<p>\u0412 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0430\u043b\u044c\u0442\u0435\u0440\u043d\u0430\u0442\u0438\u0432\u044b, \u043c\u044b \u0442\u0430\u043a\u0436\u0435 \u043c\u043e\u0436\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0438\u043c\u044f \u043a\u043e\u043d\u0446\u0435\u043f\u0442\u0430 \u0432\u043c\u0435\u0441\u0442\u043e <code>typename<\/code> \u0431\u0435\u0437 \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u0430 <code>requires<\/code>:<\/p>\n<pre><code class=\"cpp\">template &lt;std::is_floating_point T> constexpr bool close_enough20(T a, T b) {    return absolute(a - b) &lt; precision_threshold&lt;T)>; }<\/code><\/pre>\n<p>\u0412 \u044d\u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043c\u044b \u0442\u0430\u043a\u0436\u0435 \u043f\u0435\u0440\u0435\u043a\u043b\u044e\u0447\u0438\u043b\u0438\u0441\u044c \u0441 <code>is_floating_point_v<\/code> \u043d\u0430 \u043a\u043e\u043d\u0446\u0435\u043f\u0442 <code>floating_point<\/code> \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u044b\u0439 \u043f\u043e\u0434 \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043a\u043e\u043c <code>&lt;concepts><\/code>.<\/p>\n<p>\u0412\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u044d\u0442\u043e\u0442 \u043a\u043e\u0434 \u0432 <a href=\"https:\/\/godbolt.org\/z\/x5c3rdn1W\">@Compiler Explorer<\/a><\/p>\n<p>\u0425\u043e\u0440\u043e\u0448\u043e, \u0430 \u043a\u0430\u043a \u043d\u0430\u0441\u0447\u0435\u0442 \u0434\u0440\u0443\u0433\u0438\u0445 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f?<\/p>\n<h3>\u041f\u0440\u0438\u043c\u0435\u0440 2 \u2014 \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0435 \u0441\u0440\u0435\u0434\u043d\u0435\u0433\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f\u00a0<\/h3>\n<p>\u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0435\u0449\u0435 \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u043f\u043e\u0440\u0430\u0431\u043e\u0442\u0430\u0435\u043c \u0441 \u0447\u0438\u0441\u043b\u0430\u043c\u0438. \u0422\u0435\u043f\u0435\u0440\u044c \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u043f\u0438\u0441\u0430\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u044e, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0431\u0435\u0440\u0435\u0442 \u043d\u0430 \u0432\u0445\u043e\u0434 \u0447\u0438\u0441\u043b\u043e\u0432\u043e\u0439 \u0432\u0435\u043a\u0442\u043e\u0440 \u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0441\u0440\u0435\u0434\u043d\u0435\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435.<\/p>\n<p>\u0412\u043e\u0442, \u0447\u0442\u043e \u043c\u044b \u0445\u043e\u0442\u0435\u043b\u0438 \u0431\u044b \u0432\u0438\u0434\u0435\u0442\u044c:<\/p>\n<pre><code class=\"cpp\">std::vector ints { 1, 2, 3, 4, 5}; std::cout &lt;&lt; Average(ints) &lt;&lt; '\\n';<\/code><\/pre>\n<p>\u041d\u0430\u0448\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0434\u043e\u043b\u0436\u043d\u0430:<\/p>\n<ul>\n<li>\n<p>\u041f\u0440\u0438\u043d\u0438\u043c\u0430\u0442\u044c \u0447\u0438\u0441\u043b\u0430 \u0441 \u043f\u043b\u0430\u0432\u0430\u044e\u0449\u0435\u0439 \u0437\u0430\u043f\u044f\u0442\u043e\u0439 \u0438\u043b\u0438 \u0446\u0435\u043b\u043e\u0447\u0438\u0441\u043b\u0435\u043d\u043d\u044b\u0435 \u0442\u0438\u043f\u044b.<\/p>\n<\/li>\n<li>\n<p>\u0412\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c <code>double<\/code>.<\/p>\n<\/li>\n<\/ul>\n<p>\u0412 C++20 \u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u044b (ranges), \u043d\u043e \u0432 \u0446\u0435\u043b\u044f\u0445 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0438 \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0434\u0440\u0443\u0433\u0438\u0435 \u0441\u043f\u043e\u0441\u043e\u0431\u044b \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0442\u0430\u043a\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e.<\/p>\n<p>\u0412\u043e\u0442 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u0430\u044f \u0432\u0435\u0440\u0441\u0438\u044f \u0441 \u043a\u043e\u043d\u0446\u0435\u043f\u0442\u0430\u043c\u0438:<\/p>\n<pre><code class=\"cpp\">template &lt;typename T>  requires std::is_integral_v&lt;T> || std::is_floating_point_v&lt;T> constexpr double Average(const std::vector&lt;T>&amp; vec) {     const double sum = std::accumulate(vec.begin(), vec.end(), 0.0);             return sum \/ static_cast&lt;double>(vec.size()); }<\/code><\/pre>\n<p>\u0414\u043b\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0438\u0442\u044c \u0448\u0430\u0431\u043b\u043e\u043d\u043d\u044b\u0439 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440 \u0434\u043e \u0446\u0435\u043b\u044b\u0445 \u0447\u0438\u0441\u0435\u043b \u0438\u043b\u0438 \u0447\u0438\u0441\u0435\u043b \u0441 \u043f\u043b\u0430\u0432\u0430\u044e\u0449\u0435\u0439 \u0437\u0430\u043f\u044f\u0442\u043e\u0439.<\/p>\n<p>\u0423 \u043d\u0430\u0441 \u043d\u0435\u0442 \u043f\u0440\u0435\u0434\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u043e\u0433\u043e \u043a\u043e\u043d\u0446\u0435\u043f\u0442\u0430, \u043e\u0431\u044a\u0435\u0434\u0438\u043d\u044f\u044e\u0449\u0435\u0433\u043e \u0442\u0438\u043f\u044b \u0441 \u043f\u043b\u0430\u0432\u0430\u044e\u0449\u0435\u0439 \u0437\u0430\u043f\u044f\u0442\u043e\u0439 \u0438 \u0446\u0435\u043b\u043e\u0447\u0438\u0441\u043b\u0435\u043d\u043d\u044b\u0435, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043f\u043e\u043f\u0440\u043e\u0431\u043e\u0432\u0430\u0442\u044c \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0441\u0432\u043e\u0439 \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0439:<\/p>\n<pre><code class=\"cpp\">template &lt;typename T>  concept numeric = std::is_integral_v&lt;T> || std::is_floating_point_v&lt;T>;<\/code><\/pre>\n<p>\u0418 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0435\u0433\u043e \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:<\/p>\n<pre><code class=\"cpp\">template &lt;typename T>  requires numeric&lt;T> constexpr double Average2(std::vector&lt;T> const &amp;vec) {     const double sum = std::accumulate(vec.begin(), vec.end(), 0.0);             return sum \/ static_cast&lt;double>(vec.size()); }<\/code><\/pre>\n<p>\u041c\u044b \u0442\u0430\u043a\u0436\u0435 \u043c\u043e\u0436\u0435\u043c \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u044d\u0442\u043e\u0442 \u043a\u043e\u0434 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u043b\u0430\u043a\u043e\u043d\u0438\u0447\u043d\u044b\u043c:<\/p>\n<pre><code class=\"cpp\">constexpr double Average3(std::vector&lt;numeric auto> const &amp;vec) {     const double sum = std::accumulate(vec.begin(), vec.end(), 0.0);             return sum \/ static_cast&lt;double>(vec.size()); }<\/code><\/pre>\n<p>\u0422\u0430\u043a \u044d\u0442\u043e\u0442 \u043a\u043e\u0434 \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u0442\u044c \u0441 enable_if C++14:<\/p>\n<pre><code class=\"cpp\">template &lt;typename T>  std::enable_if_t&lt;std::is_integral_v&lt;T> || std::is_floating_point_v&lt;T>, double> Average4(std::vector&lt;T> const &amp;vec) {     const double sum = std::accumulate(vec.begin(), vec.end(), 0.0);             return sum \/ static_cast&lt;double>(vec.size()); }<\/code><\/pre>\n<p>\u0412\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u044d\u0442\u043e\u0442 \u043a\u043e\u0434 \u0432 <a href=\"https:\/\/godbolt.org\/z\/jjq6adG83\">@Compiler Explorer<\/a><\/p>\n<h3>\u041f\u0440\u0438\u043c\u0435\u0440 3 \u2014 \u0444\u0430\u0431\u0440\u0438\u043a\u0430 \u0441 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u043c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e\u043c \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u0432<\/h3>\n<p>\u0412 \u043f\u0430\u0440\u0430\u0433\u0440\u0430\u0444\u0435 18 \u043a\u043d\u0438\u0433\u0438 \u201c\u042d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 C++\u201d \u0421\u043a\u043e\u0442\u0442 \u041c\u0430\u0439\u0435\u0440\u0441 \u043e\u043f\u0438\u0441\u0430\u043b \u0444\u0443\u043d\u043a\u0446\u0438\u044e <code>makeInvestment<\/code>:<\/p>\n<pre><code class=\"cpp\">template&lt;typename... Ts>  std::unique_ptr&lt;Investment>  makeInvestment(Ts&amp;&amp;... params);<\/code><\/pre>\n<p>\u042d\u0442\u043e \u0444\u0430\u0431\u0440\u0438\u0447\u043d\u044b\u0439 \u043c\u0435\u0442\u043e\u0434, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0441\u043e\u0437\u0434\u0430\u0435\u0442 \u043a\u043b\u0430\u0441\u0441\u044b, \u043d\u0430\u0441\u043b\u0435\u0434\u0443\u0435\u043c\u044b\u0435 \u043e\u0442 Investment, \u0438 \u0435\u0433\u043e \u0433\u043b\u0430\u0432\u043d\u043e\u0435 \u043f\u0440\u0435\u0438\u043c\u0443\u0449\u0435\u0441\u0442\u0432\u043e \u0437\u0430\u043a\u043b\u044e\u0447\u0430\u0435\u0442\u0441\u044f \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e \u043e\u043d \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u0432!<\/p>\n<p>\u0412\u043e\u0442 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043f\u0440\u0435\u0434\u043b\u0430\u0433\u0430\u0435\u043c\u044b\u0445 \u0442\u0438\u043f\u043e\u0432 \u0434\u043b\u044f \u043f\u0440\u0438\u043c\u0435\u0440\u0430:<\/p>\n<pre><code class=\"cpp\">class Investment { public:     virtual ~Investment() { }      virtual void calcRisk() = 0; };  class Stock : public Investment { public:     explicit Stock(const std::string&amp;) { }      void calcRisk() override { } };  class Bond : public Investment { public:     explicit Bond(const std::string&amp;, const std::string&amp;, int) { }      void calcRisk() override { } };  class RealEstate : public Investment { public:     explicit RealEstate(const std::string&amp;, double, int) { }      void calcRisk() override { } };<\/code><\/pre>\n<p>\u041a\u043e\u0434 \u0438\u0437 \u043a\u043d\u0438\u0433\u0438 \u0431\u044b\u043b \u0441\u043b\u0438\u0448\u043a\u043e\u043c \u0438\u0434\u0435\u0430\u043b\u0438\u0441\u0442\u0438\u0447\u043d\u044b\u043c \u0438 \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u043d\u0435\u0440\u0430\u0431\u043e\u0447\u0438\u043c. \u041e\u043d \u0440\u0430\u0431\u043e\u0442\u0430\u043b \u0442\u043e\u043b\u044c\u043a\u043e \u0432 \u0442\u0435\u0445 \u0441\u043b\u0443\u0447\u0430\u044f\u0445, \u043a\u043e\u0433\u0434\u0430 \u0432\u0441\u0435 \u0432\u0430\u0448\u0438 \u043a\u043b\u0430\u0441\u0441\u044b \u0438\u043c\u0435\u043b\u0438 \u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0432\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0438 \u0442\u0438\u043f\u044b \u0432\u0445\u043e\u0434\u043d\u044b\u0445 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432:<\/p>\n<p><a href=\"http:\/\/www.aristeia.com\/BookErrata\/emc++-errata.html\">\u0421\u043a\u043e\u0442\u0442 \u041c\u0430\u0439\u0435\u0440\u0441: \u0418\u0441\u0442\u043e\u0440\u0438\u044f \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0439 \u0438 \u0441\u043f\u0438\u0441\u043e\u043a \u0438\u0441\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0439 \u0434\u043b\u044f \u043a\u043d\u0438\u0433\u0438 \u201c\u042d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 C++\u201d<\/a>:<\/p>\n<blockquote>\n<p>\u0418\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 <code>makeInvestment<\/code> \u043d\u0435\u0440\u0435\u0430\u043b\u0438\u0441\u0442\u0438\u0447\u0435\u043d, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u043e\u043d \u043f\u043e\u0434\u0440\u0430\u0437\u0443\u043c\u0435\u0432\u0430\u0435\u0442, \u0447\u0442\u043e \u0432\u0441\u0435 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u043d\u044b\u0435 \u0442\u0438\u043f\u044b \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 \u043c\u043e\u0433\u0443\u0442 \u0431\u044b\u0442\u044c \u0441\u043e\u0437\u0434\u0430\u043d\u044b \u0438\u0437 \u043e\u0434\u043d\u0438\u0445 \u0438 \u0442\u0435\u0445 \u0436\u0435 \u0442\u0438\u043f\u043e\u0432 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u0432. \u042d\u0442\u043e \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e \u0431\u0440\u043e\u0441\u0430\u0435\u0442\u0441\u044f \u0432 \u0433\u043b\u0430\u0437\u0430 \u0432 \u043a\u043e\u0434\u0435 \u043f\u0440\u0438\u043c\u0435\u0440\u0430 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438, \u0433\u0434\u0435 \u043d\u0430\u0448\u0438 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u044b \u043f\u0435\u0440\u0435\u0434\u0430\u044e\u0442\u0441\u044f \u0432\u0441\u0435\u043c \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\u0430\u043c \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u043d\u044b\u0445 \u043a\u043b\u0430\u0441\u0441\u043e\u0432 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043f\u0440\u044f\u043c\u043e\u0439 \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0438 (perfect-forwarding).<\/p>\n<\/blockquote>\n<p>\u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0435\u0441\u043b\u0438 \u0443 \u0432\u0430\u0441 \u0435\u0441\u0442\u044c \u043e\u0434\u0438\u043d \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440, \u043a\u043e\u0442\u043e\u0440\u043e\u043c\u0443 \u043d\u0443\u0436\u043d\u044b \u0434\u0432\u0430 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u0430, \u0438 \u0434\u0440\u0443\u0433\u043e\u0439 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 \u0441 \u0442\u0440\u0435\u043c\u044f \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u0430\u043c\u0438, \u043a\u043e\u0434 \u043c\u043e\u0436\u0435\u0442 \u043d\u0435 \u0441\u043a\u043e\u043c\u043f\u0438\u043b\u0438\u0440\u043e\u0432\u0430\u0442\u044c\u0441\u044f:<\/p>\n<pre><code class=\"cpp\">\/\/ \u043f\u0441\u0435\u0432\u0434\u043e\u043a\u043e\u0434: Bond(int, int, int) { } Stock(double, double) { } make(args...) {   if (bond)      new Bond(args...);   else if (stock)      new Stock(args...) }<\/code><\/pre>\n<p>\u0415\u0441\u043b\u0438 \u0432\u044b \u043d\u0430\u043f\u0438\u0448\u0435\u0442\u0435 <code>make(bond, 1, 2, 3)<\/code>, \u0442\u043e \u0431\u043b\u043e\u043a <code>else<\/code> \u043d\u0435 \u0431\u0443\u0434\u0435\u0442 \u0441\u043a\u043e\u043c\u043f\u0438\u043b\u0438\u0440\u043e\u0432\u0430\u043d \u2014 \u0442\u0430\u043a \u043a\u0430\u043a \u043d\u0435\u0442 \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u043e\u0433\u043e <code>Stock(1, 2, 3)<\/code>! \u0427\u0442\u043e\u0431\u044b \u044d\u0442\u043e\u0442 \u043a\u043e\u0434 \u0440\u0430\u0431\u043e\u0442\u0430\u043b, \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u0447\u0442\u043e-\u0442\u043e \u0432\u0440\u043e\u0434\u0435 \u0441\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e <code>if<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u043e\u0442\u0431\u0440\u0430\u0441\u044b\u0432\u0430\u0442\u044c \u0432\u043e \u0432\u0440\u0435\u043c\u044f \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0446\u0438\u0438 \u0447\u0430\u0441\u0442\u0438 \u043a\u043e\u0434\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043d\u0435 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0442 \u0443\u0441\u043b\u043e\u0432\u0438\u044e.<\/p>\n<p>\u041d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0441\u0442\u0430\u0442\u0435\u0439 \u043d\u0430\u0437\u0430\u0434 \u0432\u043c\u0435\u0441\u0442\u0435 \u0441 \u043e\u0434\u043d\u0438\u043c \u0438\u0437 \u043c\u043e\u0438\u0445 \u0447\u0438\u0442\u0430\u0442\u0435\u043b\u0435\u0439 \u043c\u044b \u043f\u0440\u0438\u0434\u0443\u043c\u0430\u043b\u0438 \u0440\u0430\u0431\u043e\u0442\u0430\u044e\u0449\u0435\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 (\u043f\u043e\u0434\u0440\u043e\u0431\u043d\u0435\u0435 \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u043f\u0440\u043e\u0447\u0438\u0442\u0430\u0442\u044c \u0432 <a href=\"https:\/\/www.cppstories.com\/2016\/03\/nice-c-factory-implementation-2.html\">Nice C++ Factory Implementation 2<\/a>).<\/p>\n<p>\u0412\u043e\u0442 \u043a\u043e\u0434, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c:<\/p>\n<pre><code class=\"cpp\">template &lt;typename... Ts>  unique_ptr&lt;Investment>  makeInvestment(const string &amp;name, Ts&amp;&amp;... params) {     unique_ptr&lt;Investment> pInv;      if (name == \"Stock\")         pInv = constructArgs&lt;Stock, Ts...>(forward&lt;Ts>(params)...);     else if (name == \"Bond\")         pInv = constructArgs&lt;Bond, Ts...>(forward&lt;Ts>(params)...);     else if (name == \"RealEstate\")         pInv = constructArgs&lt;RealEstate, Ts...>(forward&lt;Ts>(params)...);      \/\/ \u0434\u0430\u043b\u0435\u0435 \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u043c \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u043c\u0435\u0442\u043e\u0434\u044b \u0434\u043b\u044f \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 pInv...      return pInv; }<\/code><\/pre>\n<p>\u041a\u0430\u043a \u0432\u0438\u0434\u0438\u0442\u0435, \u0432\u0441\u044f \u201c\u043c\u0430\u0433\u0438\u044f\u201d \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u0432\u043d\u0443\u0442\u0440\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <code>convertArgs<\/code>.<\/p>\n<p>\u041e\u0441\u043d\u043e\u0432\u043d\u0430\u044f \u0438\u0434\u0435\u044f \u0437\u0430\u043a\u043b\u044e\u0447\u0430\u0435\u0442\u0441\u044f \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e\u0431\u044b \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c <code>unique_ptr&lt;Type><\/code>, \u043a\u043e\u0433\u0434\u0430 \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c Type, \u0441\u043a\u043e\u043d\u0441\u0442\u0440\u0443\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0438\u0437 \u0437\u0430\u0434\u0430\u043d\u043d\u043e\u0433\u043e \u043d\u0430\u0431\u043e\u0440\u0430 \u0430\u0442\u0440\u0438\u0431\u0443\u0442\u043e\u0432, \u0438\u043b\u0438 <code>nullptr<\/code> \u0432 \u043f\u0440\u043e\u0442\u0438\u0432\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435.<\/p>\n<h4>\u0414\u043e \u0421++17 \u00a0<\/h4>\n<p>\u0412 \u043c\u043e\u0435\u043c \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u043c \u0440\u0435\u0448\u0435\u043d\u0438\u0438 (\u0434\u043e C++17) \u043c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b\u0438 <code>std::enable_if<\/code>, \u0438 \u044d\u0442\u043e \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u043b\u043e \u0442\u0430\u043a:<\/p>\n<pre><code class=\"cpp\">\/\/ \u0434\u043e C++17 template &lt;typename Concrete, typename... Ts> enable_if_t&lt;is_constructible&lt;Concrete, Ts...>::value, unique_ptr&lt;Concrete>> constructArgsOld(Ts&amp;&amp;... params) {     return std::make_unique&lt;Concrete>(forward&lt;Ts>(params)...); }  template &lt;typename Concrete, typename... Ts> enable_if_t&lt;!is_constructible&lt;Concrete, Ts...>::value, unique_ptr&lt;Concrete> > constructArgsOld(...) {     return nullptr; }<\/code><\/pre>\n<p><a href=\"http:\/\/en.cppreference.com\/w\/cpp\/types\/is_constructible\">std::is_constructible <\/a>(<a href=\"http:\/\/en.cppreference.com\/w\/cpp\/types\/is_constructible\">\u0441\u043c\u043e\u0442\u0440\u0438\u0442\u0435 c++ reference.com<\/a>) \u2014 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043d\u0430\u043c \u0431\u044b\u0441\u0442\u0440\u043e \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u043c\u043e\u0436\u043d\u043e \u043b\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u0432 \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u0434\u0430\u043d\u043d\u043e\u0433\u043e \u0442\u0438\u043f\u0430.<\/p>\n<p>\u0412 C++17 \u0435\u0441\u0442\u044c \u0445\u0435\u043b\u043f\u0435\u0440:<\/p>\n<pre><code class=\"cpp\">is_constructible_v = is_constructible&lt;T, Args...>::value;<\/code><\/pre>\n<p>\u0422\u0430\u043a \u043c\u044b \u043c\u043e\u0433\u043b\u0438 \u0431\u044b \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u043a\u043e\u0434 \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u043b\u0430\u043a\u043e\u043d\u0438\u0447\u043d\u0435\u0435\u2026<\/p>\n<p>\u0422\u0435\u043c \u043d\u0435 \u043c\u0435\u043d\u0435\u0435, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 <code>enable_if<\/code> \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u043d\u0435\u044d\u043b\u0435\u0433\u0430\u043d\u0442\u043d\u043e \u0438 \u0447\u0435\u0440\u0435\u0441\u0447\u0443\u0440 \u0441\u043b\u043e\u0436\u043d\u043e. \u041a\u0430\u043a \u043d\u0430\u0441\u0447\u0435\u0442 C++17 \u0432\u0435\u0440\u0441\u0438\u0438?<\/p>\n<h4>\u0421 if constexpr \u00a0\u00a0<\/h4>\n<p>\u0412\u043e\u0442 \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u043d\u0430\u044f \u0432\u0435\u0440\u0441\u0438\u044f:<\/p>\n<pre><code class=\"cpp\">template &lt;typename Concrete, typename... Ts> unique_ptr&lt;Concrete> constructArgs(Ts&amp;&amp;... params) {     if constexpr (is_constructible_v&lt;Concrete, Ts...>)       return make_unique&lt;Concrete>(forward&lt;Ts>(params)...);    else        return nullptr; }<\/code><\/pre>\n<p>\u041e\u0447\u0435\u043d\u044c \u043b\u0430\u043a\u043e\u043d\u0438\u0447\u043d\u043e!<\/p>\n<p>\u041c\u044b \u043c\u043e\u0436\u0435\u043c \u0434\u0430\u0436\u0435 \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0441\u044e\u0434\u0430 \u043b\u043e\u0433\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u0441\u0432\u0435\u0440\u0442\u043a\u0443 (fold expression):<\/p>\n<pre><code class=\"cpp\">template &lt;typename Concrete, typename... Ts> std::unique_ptr&lt;Concrete> constructArgs(Ts&amp;&amp;... params) {      cout &lt;&lt; __func__ &lt;&lt; \": \";     \/\/ \u0441\u0432\u0435\u0440\u0442\u043a\u0430:     ((cout &lt;&lt; params &lt;&lt; \", \"), ...);     cout &lt;&lt; \"\\n\";      if constexpr (std::is_constructible_v&lt;Concrete, Ts...>)         return make_unique&lt;Concrete>(forward&lt;Ts>(params)...);     else        return nullptr; }<\/code><\/pre>\n<p>\u041d\u0435\u043f\u043b\u043e\u0445\u043e\u2026 \u043d\u0435 \u0442\u0430\u043a \u043b\u0438? \ud83d\ude42<\/p>\n<p>\u0412\u0441\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0441\u0430 <code>enable_if<\/code> \u043a\u0430\u043d\u0443\u043b\u0430 \u0432 \u043b\u0435\u0442\u0443; \u043d\u0430\u043c \u0434\u0430\u0436\u0435 \u043d\u0435 \u043d\u0443\u0436\u043d\u0430 \u043f\u0435\u0440\u0435\u0433\u0440\u0443\u0437\u043a\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0434\u043b\u044f <code>else<\/code>. \u0422\u0435\u043f\u0435\u0440\u044c \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0437\u0430\u043a\u043b\u044e\u0447\u0438\u0442\u044c \u0432\u0435\u0441\u044c \u044d\u0442\u043e\u0442 \u0432\u044b\u0440\u0430\u0437\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043a\u043e\u0434 \u0432 \u043e\u0434\u043d\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438.<\/p>\n<p>if constexpr \u0432\u044b\u0447\u0438\u0441\u043b\u044f\u0435\u0442 \u0443\u0441\u043b\u043e\u0432\u0438\u0435, \u0432 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435 \u0447\u0435\u0433\u043e \u0431\u0443\u0434\u0435\u0442 \u0441\u043a\u043e\u043c\u043f\u0438\u043b\u0438\u0440\u043e\u0432\u0430\u043d \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u0438\u043d \u0431\u043b\u043e\u043a. \u0412 \u043d\u0430\u0448\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0435, \u0435\u0441\u043b\u0438 \u0442\u0438\u043f \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0441\u043a\u043e\u043d\u0441\u0442\u0440\u0443\u0438\u0440\u043e\u0432\u0430\u043d \u0438\u0437 \u0437\u0430\u0434\u0430\u043d\u043d\u043e\u0433\u043e \u043d\u0430\u0431\u043e\u0440\u0430 \u0430\u0442\u0440\u0438\u0431\u0443\u0442\u043e\u0432, \u043c\u044b \u0441\u043a\u043e\u043c\u043f\u0438\u043b\u0438\u0440\u0443\u0435\u043c <code>make_unique<\/code>. \u0415\u0441\u043b\u0438 \u043d\u0435\u0442, \u0442\u043e \u0432\u0435\u0440\u043d\u0435\u043c <code>nullptr<\/code> (\u0438 <code>make_unique<\/code> \u0434\u0430\u0436\u0435 \u043d\u0435 \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u0437\u0434\u0430\u043d\u0430).<\/p>\n<h4>C++20 \u00a0\u00a0<\/h4>\n<p>\u041c\u044b \u043c\u043e\u0436\u0435\u043c \u043b\u0435\u0433\u043a\u043e \u0437\u0430\u043c\u0435\u043d\u0438\u0442\u044c <code>enable_if<\/code> \u043a\u043e\u043d\u0446\u0435\u043f\u0442\u0430\u043c\u0438:<\/p>\n<pre><code class=\"cpp\">\/\/ C++20: template &lt;typename Concrete, typename... Ts> requires std::is_constructible_v&lt;Concrete, Ts...> std::unique_ptr&lt;Concrete> constructArgs20(Ts&amp;&amp;... params) {     return std::make_unique&lt;Concrete>(std::forward&lt;Ts>(params)...); }  template &lt;typename Concrete, typename... Ts> std::unique_ptr&lt;Concrete> constructArgs20(...) {     return nullptr; }<\/code><\/pre>\n<p>\u041d\u043e \u044f \u043d\u0435 \u0443\u0432\u0435\u0440\u0435\u043d, \u0447\u0442\u043e \u044d\u0442\u043e\u0442 \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u043b\u0443\u0447\u0448\u0435. \u042f \u0434\u0443\u043c\u0430\u044e, \u0447\u0442\u043e \u0432 \u044d\u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435, <code>if constexpr<\/code> \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u043d\u0430\u043c\u043d\u043e\u0433\u043e \u043b\u0430\u043a\u043e\u043d\u0438\u0447\u043d\u0435\u0435 \u0438 \u043f\u0440\u043e\u0449\u0435 \u0434\u043b\u044f \u043f\u043e\u043d\u0438\u043c\u0430\u043d\u0438\u044f.<\/p>\n<p>\u0412\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u044d\u0442\u043e\u0442 \u043a\u043e\u0434 \u0432 <a href=\"https:\/\/godbolt.org\/z\/z6Tqzbd73\">@Compiler Explorer<\/a><\/p>\n<h3>\u041f\u0440\u0438\u043c\u0435\u0440 4 \u2014 \u0440\u0435\u0430\u043b\u044c\u043d\u044b\u0435 \u043f\u0440\u043e\u0435\u043a\u0442\u044b \u00a0\u00a0\u00a0\u00a0<\/h3>\n<p><code>if constexpr<\/code> \u0433\u043e\u0434\u0438\u0442\u0441\u044f \u043d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u0434\u043b\u044f \u044d\u043a\u0441\u043f\u0435\u0440\u0438\u043c\u0435\u043d\u0442\u0430\u043b\u044c\u043d\u044b\u0445 \u0434\u0435\u043c\u043e\u043a \u2014 \u043e\u043d \u0443\u0436\u0435 \u043d\u0430\u0448\u0435\u043b \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u0435 \u0432 \u043f\u0440\u043e\u0434\u0430\u043a\u0448\u0435\u043d\u0435.<\/p>\n<p>\u0415\u0441\u043b\u0438 \u043c\u044b \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043d\u0430 \u043e\u043f\u0435\u043d\u0441\u043e\u0440\u0441\u043d\u0443\u044e \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e STL \u043e\u0442 \u043a\u043e\u043c\u0430\u043d\u0434\u044b MSVC, \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043d\u0430\u0439\u0442\u0438 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0441\u043b\u0443\u0447\u0430\u0435\u0432, \u0433\u0434\u0435 <code>if constexpr<\/code> \u043f\u0440\u0438\u0448\u0435\u043b\u0441\u044f \u043e\u0447\u0435\u043d\u044c \u043a\u0441\u0442\u0430\u0442\u0438.<\/p>\n<p>\u0416\u0443\u0440\u043d\u0430\u043b \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0439 \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u0437\u0434\u0435\u0441\u044c: <a href=\"https:\/\/github.com\/microsoft\/STL\/wiki\/Changelog\">https:\/\/github.com\/microsoft\/STL\/wiki\/Changelog<\/a><\/p>\n<p>\u0412\u043e\u0442 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0438\u0437 \u0443\u043b\u0443\u0447\u0448\u0435\u043d\u0438\u0439:<\/p>\n<ul>\n<li>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0432\u043c\u0435\u0441\u0442\u043e \u0441\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u0434\u0438\u0441\u043f\u0435\u0442\u0447\u0435\u0440\u0438\u0437\u0430\u0446\u0438\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f <code>if constexpr<\/code> \u0432: <code>get&lt;I>()<\/code> \u0438 <code>get&lt;T>()<\/code> \u0434\u043b\u044f pair (<a href=\"https:\/\/github.com\/microsoft\/STL\/pull\/2756\">#2756<\/a>)<\/p>\n<\/li>\n<li>\n<p>\u0412\u043c\u0435\u0441\u0442\u043e \u0441\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u0434\u0438\u0441\u043f\u0435\u0442\u0447\u0435\u0440\u0438\u0437\u0430\u0446\u0438\u0438, \u043f\u0435\u0440\u0435\u0433\u0440\u0443\u0437\u043e\u043a \u0438\u043b\u0438 \u0441\u043f\u0435\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0439 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f <code>if constexpr<\/code> \u0432 \u0442\u0430\u043a\u0438\u0445 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430\u0445, \u043a\u0430\u043a <code>is_permutation()<\/code>, <code>sample()<\/code>, <code>rethrow_if_nested()<\/code> \u0438 <code>default_searcher<\/code> (#2219), \u043e\u0431\u0449\u0438\u0445 \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c\u0430\u0445 <code>&lt;map><\/code> \u0438 <code>&lt;set><\/code> (#2287) \u0438 \u043f\u0430\u0440\u0435 \u0434\u0440\u0443\u0433\u0438\u0445 \u043c\u0435\u0441\u0442.<\/p>\n<\/li>\n<li>\n<p>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f\u00a0<code>if constexpr<\/code> \u0432\u043c\u0435\u0441\u0442\u043e \u0441\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u0434\u0438\u0441\u043f\u0435\u0442\u0447\u0435\u0440\u0438\u0437\u0430\u0446\u0438\u0438 \u0432 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0438 \u0432 <code>find()<\/code> (#2380), <code>basic_string(first, last)<\/code> (#2480)<\/p>\n<\/li>\n<li>\n<p>\u0423\u043b\u0443\u0447\u0448\u0435\u043d\u0430 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0432\u0435\u043a\u0442\u043e\u0440\u0430, \u0442\u0430\u043a\u0436\u0435 \u0434\u043b\u044f \u0443\u043f\u0440\u043e\u0449\u0435\u043d\u0438\u044f \u043a\u043e\u0434\u0430 \u0431\u044b\u043b \u0437\u0430\u0434\u0435\u0439\u0441\u0442\u0432\u043e\u0432\u0430\u043d <code>if constexpr<\/code> (#1771)<\/p>\n<\/li>\n<\/ul>\n<p>\u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043d\u0430 \u0443\u043b\u0443\u0447\u0448\u0435\u043d\u0438\u044f \u0434\u043b\u044f <code>std::pair<\/code>:<\/p>\n<p><a href=\"https:\/\/github.com\/microsoft\/STL\/pull\/2756\">Untag dispatch get for pair by frederick-vs-ja \u00b7 Pull Request #2756 \u00b7 microsoft\/STL<\/a><\/p>\n<p>\u0414\u043e C++17 \u043a\u043e\u0434 \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u043b \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:<\/p>\n<pre><code class=\"cpp\">template &lt;class _Ret, class _Pair> constexpr _Ret _Pair_get(_Pair&amp; _Pr, integral_constant&lt;size_t, 0>) noexcept {     \/\/ \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0441\u0441\u044b\u043b\u043a\u0443 \u043d\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 0 \u0432 \u043f\u0430\u0440\u0435 _Pr     return _Pr.first; }  template &lt;class _Ret, class _Pair> constexpr _Ret _Pair_get(_Pair&amp; _Pr, integral_constant&lt;size_t, 1>) noexcept {     \/\/ \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0441\u0441\u044b\u043b\u043a\u0443 \u043d\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 1 \u0432 \u043f\u0430\u0440\u0435 _Pr     return _Pr.second; }  template &lt;size_t _Idx, class _Ty1, class _Ty2> _NODISCARD constexpr tuple_element_t&lt;_Idx, pair&lt;_Ty1, _Ty2>>&amp;      get(pair&lt;_Ty1, _Ty2>&amp; _Pr) noexcept {     \/\/ \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0441\u0441\u044b\u043b\u043a\u0443 \u043d\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043f\u043e \u0430\u0434\u0440\u0435\u0441\u0443 _Idx \u0432 \u043f\u0430\u0440\u0435 _Pr     using _Rtype = tuple_element_t&lt;_Idx, pair&lt;_Ty1, _Ty2>>&amp;;     return _Pair_get&lt;_Rtype>(_Pr, integral_constant&lt;size_t, _Idx>{}); }<\/code><\/pre>\n<p>\u0418 \u043f\u043e\u0441\u043b\u0435 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f:<\/p>\n<pre><code class=\"cpp\">template &lt;size_t _Idx, class _Ty1, class _Ty2> _NODISCARD constexpr tuple_element_t&lt;_Idx, pair&lt;_Ty1, _Ty2>>&amp; get(pair&lt;_Ty1, _Ty2>&amp; _Pr) noexcept {     \/\/ \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0441\u0441\u044b\u043b\u043a\u0443 \u043d\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043f\u043e \u0430\u0434\u0440\u0435\u0441\u0443 _Idx \u0432 \u043f\u0430\u0440\u0435 _Pr     if constexpr (_Idx == 0) {         return _Pr.first;     } else {         return _Pr.second;     } }<\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u044d\u0442\u043e \u043e\u0434\u043d\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u044f, \u0438 \u0435\u0435 \u043d\u0430\u043c\u043d\u043e\u0433\u043e \u043b\u0435\u0433\u0447\u0435 \u0447\u0438\u0442\u0430\u0442\u044c! \u0411\u043e\u043b\u044c\u0448\u0435 \u043d\u0435\u0442 \u043d\u0438\u043a\u0430\u043a\u043e\u0439 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0443\u044e \u0434\u0438\u0441\u043f\u0435\u0442\u0447\u0435\u0440\u0438\u0437\u0430\u0446\u0438\u044e \u0438 \u0445\u0435\u043b\u043f\u0435\u0440 <code>integer_constant<\/code>.<\/p>\n<p>\u0412 \u0434\u0440\u0443\u0433\u043e\u0439 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0435, \u043d\u0430 \u044d\u0442\u043e\u0442 \u0440\u0430\u0437 \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u043e\u0439 \u0441 SIMD-\u0442\u0438\u043f\u0430\u043c\u0438 \u0438 \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044f\u043c\u0438 (\u043f\u043e\u043f\u0443\u043b\u044f\u0440\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043e\u0442 Agner Fog), \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u043d\u0430\u0439\u0442\u0438 \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e \u043f\u0440\u0438\u043c\u0435\u0440\u043e\u0432 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f <code>if constexpr<\/code>:<\/p>\n<p><a href=\"https:\/\/github.com\/vectorclass\/version2\/blob\/master\/instrset.h\">https:\/\/github.com\/vectorclass\/version2\/blob\/master\/instrset.h<\/a><\/p>\n<p>\u041e\u0434\u043d\u0438\u043c \u0438\u0437 \u044f\u0440\u043a\u0438\u0445 \u043f\u0440\u0438\u043c\u0435\u0440\u043e\u0432 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u043c\u0430\u0441\u043a\u0438:<\/p>\n<pre><code class=\"cpp\">\/\/ zero_mask: \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u043a\u043e\u043c\u043f\u0430\u043a\u0442\u043d\u0443\u044e \u0431\u0438\u0442\u043e\u0432\u0443\u044e \u043c\u0430\u0441\u043a\u0443 \u0434\u043b\u044f \u043e\u0431\u043d\u0443\u043b\u0435\u043d\u0438\u044f \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c \u043c\u0430\u0441\u043a\u0438 AVX512. \/\/ \u041f\u0430\u0440\u0430\u043c\u0435\u0442\u0440 a \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0441\u0441\u044b\u043b\u043a\u043e\u0439 \u043d\u0430 \u043c\u0430\u0441\u0441\u0438\u0432 constexpr int \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u0432 \u043f\u0435\u0440\u0435\u0441\u0442\u0430\u043d\u043e\u0432\u043e\u043a template &lt;int N> constexpr auto zero_mask(int const (&amp;a)[N]) {     uint64_t mask = 0;     int i = 0;      for (i = 0; i &lt; N; i++) {         if (a[i] >= 0) mask |= uint64_t(1) &lt;&lt; i;     }     if constexpr      (N &lt;= 8 ) return uint8_t(mask);     else if constexpr (N &lt;= 16) return uint16_t(mask);     else if constexpr (N &lt;= 32) return uint32_t(mask);     else return mask; }<\/code><\/pre>\n<p>\u0411\u0435\u0437 <code>if constexpr<\/code> \u043a\u043e\u0434 \u0431\u044b\u043b \u0431\u044b \u043d\u0430\u043c\u043d\u043e\u0433\u043e \u0434\u043b\u0438\u043d\u043d\u0435\u0435 \u0438 \u043f\u043e\u0442\u0435\u043d\u0446\u0438\u0430\u043b\u044c\u043d\u043e \u0434\u0443\u0431\u043b\u0438\u0440\u043e\u0432\u0430\u043b\u0441\u044f \u0431\u044b.<\/p>\n<h3>\u0417\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/h3>\n<p><code>if<\/code> \u0432\u043e \u0432\u0440\u0435\u043c\u044f \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0446\u0438\u0438 \u2014 \u0437\u0430\u043c\u0435\u0447\u0430\u0442\u0435\u043b\u044c\u043d\u0430\u044f \u0444\u0438\u0447\u0430, \u0437\u043d\u0430\u0447\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0443\u043f\u0440\u043e\u0449\u0430\u044e\u0449\u0430\u044f \u0448\u0430\u0431\u043b\u043e\u043d\u043d\u044b\u0439 \u043a\u043e\u0434. \u0411\u043e\u043b\u0435\u0435 \u0442\u043e\u0433\u043e, \u043e\u043d\u0430 \u043d\u0430\u043c\u043d\u043e\u0433\u043e \u0432\u044b\u0440\u0430\u0437\u0438\u0442\u0435\u043b\u044c\u043d\u0435\u0435 \u0438 \u044d\u043b\u0435\u0433\u0430\u043d\u0442\u043d\u0435\u0435, \u0447\u0435\u043c \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u044f: \u0441\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u0434\u0438\u0441\u043f\u0435\u0442\u0447\u0435\u0440\u0438\u0437\u0430\u0446\u0438\u044f \u0438 <code>enable_if (SFINAE)<\/code>. \u0422\u0435\u043f\u0435\u0440\u044c \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043b\u0435\u0433\u043a\u043e \u0432\u044b\u0440\u0430\u0437\u0438\u0442\u044c \u0441\u0432\u043e\u0438 \u043d\u0430\u043c\u0435\u0440\u0435\u043d\u0438\u044f \u043d\u0430\u043f\u043e\u0434\u043e\u0431\u0438\u0438 \u0441 \u0440\u0430\u043d\u0442\u0430\u0439\u043c-\u043a\u043e\u0434\u043e\u043c.<\/p>\n<p>\u041c\u044b \u0442\u0430\u043a\u0436\u0435 \u043f\u0435\u0440\u0435\u0440\u0430\u0431\u043e\u0442\u0430\u043b\u0438 \u043a\u043e\u0434 \u043f\u0440\u0438\u043c\u0435\u0440\u043e\u0432 \u0441 \u043d\u043e\u0432\u043e\u0432\u0432\u0435\u0434\u0435\u043d\u0438\u044f\u043c\u0438 C++20! \u041a\u0430\u043a \u0432\u0438\u0434\u0438\u0442\u0435, \u0431\u043b\u0430\u0433\u043e\u0434\u0430\u0440\u044f \u043a\u043e\u043d\u0446\u0435\u043f\u0442\u0430\u043c \u043a\u043e\u0434 \u0441\u0442\u0430\u043b \u0435\u0449\u0435 \u0431\u043e\u043b\u0435\u0435 \u0447\u0438\u0442\u0430\u0431\u0435\u043b\u044c\u043d\u044b\u043c, \u0432\u0435\u0434\u044c \u0442\u0435\u043f\u0435\u0440\u044c \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0432\u044b\u0440\u0430\u0437\u0438\u0442\u044c \u0442\u0440\u0435\u0431\u043e\u0432\u0430\u043d\u0438\u044f \u043a \u0441\u0432\u043e\u0438\u043c \u0442\u0438\u043f\u0430\u043c \u201c\u0435\u0441\u0442\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u043c\u201d \u043e\u0431\u0440\u0430\u0437\u043e\u043c. \u041c\u044b \u0442\u0430\u043a\u0436\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u0438 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0441\u043e\u043a\u0440\u0430\u0449\u0435\u043d\u0438\u0439 \u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0441\u0430 \u0438 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u0432 \u0441\u043e\u043e\u0431\u0449\u0438\u0442\u044c \u043e \u043d\u0430\u0448\u0438\u0445 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f\u0445.<\/p>\n<p>\u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u043c\u044b \u043a\u043e\u0441\u043d\u0443\u043b\u0438\u0441\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u0441\u0430\u043c\u044b\u0445 \u0431\u0430\u0437\u043e\u0432\u044b\u0445 \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u0439, \u0438 \u044f, \u043a\u0430\u043a \u0432\u0441\u0435\u0433\u0434\u0430, \u043f\u0440\u0438\u0437\u044b\u0432\u0430\u044e \u0432\u0430\u0441 \u0441\u0430\u043c\u0438\u0445 \u0438\u0441\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u044c \u044d\u0442\u0443 \u043d\u043e\u0432\u0443\u044e \u0444\u0438\u0447\u0443.<\/p>\n<h3>\u0412\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u044f\u0441\u044c \u043d\u0430\u0437\u0430\u0434\u2026<\/h3>\n<p>\u0418 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u044f\u0441\u044c \u043a \u043d\u0430\u0448\u0435\u043c\u0443 \u043f\u0440\u0438\u043c\u0435\u0440\u0443 \u0441\u043e <code>str<\/code>:<\/p>\n<p>\u041c\u043e\u0436\u0435\u0442\u0435 \u043b\u0438 \u0432\u044b \u0442\u0435\u043f\u0435\u0440\u044c \u043f\u0435\u0440\u0435\u043f\u0438\u0441\u0430\u0442\u044c <code>str<\/code> (\u0438\u0437 \u043d\u0430\u0447\u0430\u043b\u0430 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0438), \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f <code>if constexpr<\/code>? \ud83d\ude42 \u041f\u043e\u043f\u0440\u043e\u0431\u0443\u0439\u0442\u0435, \u0430 \u0437\u0430\u0442\u0435\u043c \u0432\u0437\u0433\u043b\u044f\u043d\u0438\u0442\u0435 \u043d\u0430 \u043c\u043e\u0435 \u043f\u0440\u043e\u0441\u0442\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0432 <a href=\"https:\/\/godbolt.org\/z\/59783WooM\">@CE<\/a>.<\/p>\n<h3>\u0415\u0449\u0435 \u0431\u043e\u043b\u044c\u0448\u0435<\/h3>\n<p>\u0412\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u043d\u0430\u0439\u0442\u0438 \u0431\u043e\u043b\u044c\u0448\u0435 \u043f\u0440\u0438\u043c\u0435\u0440\u043e\u0432 \u0438 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f <code>if constexpr<\/code> \u0432 \u043c\u043e\u0435\u0439 \u043a\u043d\u0438\u0433\u0435 \u043e C++17: <a href=\"https:\/\/leanpub.com\/cpp17indetail?utm_source=blog&amp;utm_campaign=article\">C++17 \u0432 \u0434\u0435\u0442\u0430\u043b\u044f\u0445 <\/a>\u043d\u0430 <a href=\"https:\/\/leanpub.com\/cpp17indetail?utm_source=blog&amp;utm_campaign=article\">@Leanpub<\/a> \u0438\u043b\u0438 \u043f\u0435\u0447\u0430\u0442\u043d\u0443\u044e \u0432\u0435\u0440\u0441\u0438\u044e \u043d\u0430 <a href=\"https:\/\/amzn.to\/2Lt75s2\">@Amazon<\/a>.<\/p>\n<hr\/>\n<blockquote>\n<p>\u0421\u0442\u0430\u0442\u044c\u044f \u043f\u043e\u0434\u0433\u043e\u0442\u043e\u0432\u043b\u0435\u043d\u0430 \u0432 \u043f\u0440\u0435\u0434\u0434\u0432\u0435\u0440\u0438\u0438 \u0441\u0442\u0430\u0440\u0442\u0430 \u043a\u0443\u0440\u0441\u0430 &#171;<strong>C++ Developer. Professional<\/strong>&#171;. \u0412\u0441\u0435\u0445 \u0436\u0435\u043b\u0430\u044e\u0449\u0438\u0445 \u043f\u0440\u0438\u0433\u043b\u0430\u0448\u0430\u0435\u043c \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u0437\u0430\u043f\u0438\u0441\u044c \u043e\u0442\u043a\u0440\u044b\u0442\u043e\u0433\u043e \u0443\u0440\u043e\u043a\u0430 \u00ab\u0423\u043c\u043d\u044b\u0435 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u0438\u00bb, \u043d\u0430 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0440\u0430\u0437\u043e\u0431\u0440\u0430\u043b\u0438, \u0447\u0442\u043e \u0442\u0430\u043a\u043e\u0435 \u0443\u043c\u043d\u044b\u0435 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u0438 \u0438 \u0437\u0430\u0447\u0435\u043c \u043e\u043d\u0438 \u043d\u0443\u0436\u043d\u044b; \u0430 \u0442\u0430\u043a\u0436\u0435 \u043f\u0440\u043e\u0432\u0435\u043b\u0438 \u043e\u0431\u0437\u043e\u0440 \u0443\u043c\u043d\u044b\u0445 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u0435\u0439, \u0432\u0445\u043e\u0434\u044f\u0449\u0438\u0445 \u0432 <code>stl<\/code>: <code>unique_ptr<\/code>, <code>shared_ptr<\/code>, <code>weak_ptr<\/code>. \u041f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u043c\u043e\u0436\u043d\u043e <a href=\"https:\/\/otus.pw\/BUFj\/\"><strong>\u043f\u043e \u0441\u0441\u044b\u043b\u043a\u0435<\/strong>.<\/a><\/p>\n<\/blockquote>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"v-portal\" style=\"display:none;\"><\/div>\n<\/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\/company\/otus\/blog\/688688\/\"> https:\/\/habr.com\/ru\/company\/otus\/blog\/688688\/<\/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<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p>\u0414\u043e C++17 \u0443 \u043d\u0430\u0441 \u0431\u044b\u043b\u043e \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u043d\u0435\u044d\u043b\u0435\u0433\u0430\u043d\u0442\u043d\u044b\u0445 \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u0432 \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c <code>static if<\/code> (if, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0432\u043e \u0432\u0440\u0435\u043c\u044f \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0446\u0438\u0438). \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0443\u044e \u0434\u0438\u0441\u043f\u0435\u0442\u0447\u0435\u0440\u0438\u0437\u0430\u0446\u0438\u044e \u0438\u043b\u0438 SFINAE. \u041a \u0441\u0447\u0430\u0441\u0442\u044c\u044e, \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u044f \u0438\u0437\u043c\u0435\u043d\u0438\u043b\u0430\u0441\u044c \u043a \u043b\u0443\u0447\u0448\u0435\u043c\u0443, \u0432\u0435\u0434\u044c \u0442\u0435\u043f\u0435\u0440\u044c \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e <code>if constexpr<\/code>\u00a0 \u0438 \u043a\u043e\u043d\u0446\u0435\u043f\u0442\u0430\u043c\u0438 C++20!<\/p>\n<p>\u041d\u0443 \u0447\u0442\u043e \u0436, \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u0440\u0430\u0437\u0431\u0435\u0440\u0435\u043c\u0441\u044f, \u043a\u0430\u043a \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u044d\u0442\u043e \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0437\u0430\u043c\u0435\u043d\u044b <code>std::enable_if<\/code> \u043a\u043e\u0434\u0430!<\/p>\n<ul>\n<li>\n<p><em>\u041e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u0435 \u043e\u0442 \u0430\u043f\u0440\u0435\u043b\u044f 2021 \u0433.:<\/em> \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f, \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u044b\u0435 \u0441 C++20 \u2014 \u043a\u043e\u043d\u0446\u0435\u043f\u0442\u044b.<\/p>\n<\/li>\n<li>\n<p><em>\u041e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u0435 \u043e\u0442 \u0430\u0432\u0433\u0443\u0441\u0442\u0430 2022 \u0433.:<\/em> \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u043f\u0440\u0438\u043c\u0435\u0440\u044b <code>if constexpr<\/code> (\u0447\u0435\u0442\u0432\u0435\u0440\u0442\u044b\u0439).<\/p>\n<\/li>\n<\/ul>\n<h3>\u0412\u0432\u0435\u0434\u0435\u043d\u0438\u0435 \u00a0<\/h3>\n<p>If \u0432\u043e \u0432\u0440\u0435\u043c\u044f \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0446\u0438\u0438 \u0432 \u0444\u043e\u0440\u043c\u0435 <code>if constexpr<\/code> \u2014 \u044d\u0442\u043e \u0437\u0430\u043c\u0435\u0447\u0430\u0442\u0435\u043b\u044c\u043d\u0430\u044f \u0444\u0438\u0447\u0430, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0431\u044b\u043b\u0430 \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0430 \u0432 C++17. \u042d\u0442\u043e\u0442 \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b \u043c\u043e\u0436\u0435\u0442 \u043f\u043e\u043c\u043e\u0447\u044c \u043d\u0430\u043c \u0437\u043d\u0430\u0447\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0443\u043b\u0443\u0447\u0448\u0438\u0442\u044c \u0447\u0438\u0442\u0430\u0435\u043c\u043e\u0441\u0442\u044c \u043a\u043e\u0434\u0430 \u0441 \u0431\u043e\u043b\u044c\u0448\u0438\u043c\u0438 \u043d\u0430\u0433\u0440\u043e\u043c\u043e\u0436\u0434\u0435\u043d\u0438\u044f\u043c\u0438 \u0448\u0430\u0431\u043b\u043e\u043d\u043e\u0432.<\/p>\n<p>\u041a\u0440\u043e\u043c\u0435 \u0442\u043e\u0433\u043e, C++20 \u043f\u0440\u0438\u043d\u0435\u0441 \u043d\u0430\u043c \u043a\u043e\u043d\u0446\u0435\u043f\u0442\u044b (\u0441oncepts)! \u042d\u0442\u043e \u0435\u0449\u0435 \u043e\u0434\u0438\u043d \u0448\u0430\u0433 \u043d\u0430 \u043f\u0443\u0442\u0438 \u043a \u0434\u043e\u0441\u0442\u0438\u0436\u0435\u043d\u0438\u044e \u043f\u043e\u0447\u0442\u0438 \u201c\u043e\u0440\u0433\u0430\u043d\u0438\u0447\u043d\u043e\u0433\u043e\u201d \u043a\u043e\u0434\u0430 \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0446\u0438\u0438.<\/p>\n<p>\u041d\u0430 \u044d\u0442\u0443 \u0441\u0442\u0430\u0442\u044c\u044e \u043c\u0435\u043d\u044f \u0432\u0434\u043e\u0445\u043d\u043e\u0432\u0438\u043b \u043f\u043e\u0441\u0442 \u043d\u0430 <a href=\"https:\/\/meetingcpp.com\/blog\/items\/How-if-constexpr-simplifies-your-code-in-Cpp17.html\">@Meeting C++<\/a> \u0441 \u043e\u0447\u0435\u043d\u044c \u043f\u043e\u0445\u043e\u0436\u0438\u043c \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u0435\u043c. \u042f \u043d\u0430\u0448\u0435\u043b \u0435\u0449\u0435 \u0447\u0435\u0442\u044b\u0440\u0435 \u043f\u0440\u0438\u043c\u0435\u0440\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043d\u0430\u0433\u043b\u044f\u0434\u043d\u043e \u043f\u0440\u043e\u0434\u0435\u043c\u043e\u043d\u0441\u0442\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0432 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0438 \u044d\u0442\u043e\u0442 \u043d\u043e\u0432\u044b\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b:<\/p>\n<ul>\n<li>\n<p>\u0421\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u0435 \u0447\u0438\u0441\u0435\u043b<\/p>\n<\/li>\n<li>\n<p>(<strong>\u041d\u043e\u0432\u0438\u043d\u043a\u0430!<\/strong>) \u0412\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0435 \u0441\u0440\u0435\u0434\u043d\u0435\u0433\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0432 \u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440\u0435<\/p>\n<\/li>\n<li>\n<p>\u0424\u0430\u0431\u0440\u0438\u043a\u0438 \u0441 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u043c \u0447\u0438\u0441\u043b\u043e\u043c \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u0432<\/p>\n<\/li>\n<li>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440\u044b \u0440\u0435\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u043a\u043e\u0434\u0430 \u0438\u0437 \u043f\u0440\u043e\u0434\u0430\u043a\u0448\u0435\u043d\u0430\u00a0<\/p>\n<\/li>\n<\/ul>\n<p>\u041d\u043e \u0434\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430, \u0447\u0442\u043e\u0431\u044b \u0443 \u043d\u0430\u0441 \u0431\u044b\u043b\u043e \u0431\u043e\u043b\u044c\u0448\u0435 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430, \u044f \u0432\u0441\u0435-\u0442\u0430\u043a\u0438 \u0445\u043e\u0442\u0435\u043b \u0431\u044b \u043f\u0440\u043e\u0439\u0442\u0438\u0441\u044c \u043f\u043e \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u043c \u0431\u0430\u0437\u043e\u0432\u044b\u043c \u0441\u0432\u0435\u0434\u0435\u043d\u0438\u044f\u043c \u043e <code>enable_if<\/code>.<\/p>\n<h3>\u0414\u043b\u044f \u0447\u0435\u0433\u043e \u043c\u043e\u0436\u0435\u0442 \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u0442\u044c\u0441\u044f if \u0432\u043e \u0432\u0440\u0435\u043c\u044f \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0446\u0438\u0438?\u00a0 \u00a0\u00a0<\/h3>\n<p>\u041d\u0430\u0447\u043d\u0435\u043c \u0441 \u043f\u0440\u0438\u043c\u0435\u0440\u0430, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u043c\u044b \u043f\u044b\u0442\u0430\u0435\u043c\u0441\u044f \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u0432\u043e\u0434 \u0432 \u0441\u0442\u0440\u043e\u043a\u0443:<\/p>\n<pre><code class=\"cpp\">#include &lt;string> #include &lt;iostream>  template &lt;typename T> std::string str(T t) {     return std::to_string(t); }  std::string str(const std::string&amp; s) {     return s; }  std::string str(const char* s) {     return s; }  std::string str(bool b) {     return b ? \"true\" : \"false\"; }  int main() {     std::cout &lt;&lt; str(\"hello\") &lt;&lt; '\\n';     std::cout &lt;&lt; str(std::string{\"hi!\"}) &lt;&lt; '\\n';     std::cout &lt;&lt; str(42) &lt;&lt; '\\n';     std::cout &lt;&lt; str(42.2) &lt;&lt; '\\n';     std::cout &lt;&lt; str(true) &lt;&lt; '\\n'; }<\/code><\/pre>\n<p>\u0412\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u044d\u0442\u043e\u0442 \u043a\u043e\u0434 \u0432 <a href=\"https:\/\/godbolt.org\/z\/Y9q34e4ab\">Compiler Explorer<\/a>.<\/p>\n<p>\u0412 \u043a\u043e\u0434\u0435, \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d\u043d\u043e\u043c \u0432\u044b\u0448\u0435, \u043c\u044b \u0432\u0438\u0434\u0438\u043c \u0442\u0440\u0438 \u043f\u0435\u0440\u0435\u0433\u0440\u0443\u0437\u043a\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u0439 \u0434\u043b\u044f \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u044b\u0445 \u0442\u0438\u043f\u043e\u0432 \u0438 \u043e\u0434\u0438\u043d \u0448\u0430\u0431\u043b\u043e\u043d \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0434\u043b\u044f \u0432\u0441\u0435\u0445 \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0445 \u0442\u0438\u043f\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0434\u043e\u043b\u0436\u043d\u044b \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0442\u044c <code>to_string()<\/code>. \u041f\u043e\u0445\u043e\u0436\u0435, \u0447\u0442\u043e \u044d\u0442\u043e\u0442 \u043a\u043e\u0434 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442. \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u0432\u0441\u0435 \u044d\u0442\u043e \u0432 \u043e\u0434\u043d\u0443 \u0444\u0443\u043d\u043a\u0446\u0438\u044e.<\/p>\n<p>\u0421\u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043b\u0438 \u0437\u0434\u0435\u0441\u044c \u201c\u0437\u0430\u0443\u0440\u044f\u0434\u043d\u044b\u0439\u201d <code>if<\/code>?<\/p>\n<p>\u0412\u043e\u0442 \u0442\u0435\u0441\u0442\u043e\u0432\u044b\u0439 \u043a\u043e\u0434:<\/p>\n<pre><code class=\"cpp\">template &lt;typename T> std::string str(T t) {     if (std::is_convertible_v&lt;T, std::string>)         return t;     else if (std::is_same_v&lt;T, bool>)         return t ? \"true\" : \"false\";     else         return std::to_string(t); }<\/code><\/pre>\n<p>\u0417\u0432\u0443\u0447\u0438\u0442 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u043f\u0440\u043e\u0441\u0442\u043e\u2026 \u043d\u043e \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u0441\u043a\u043e\u043c\u043f\u0438\u043b\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u044d\u0442\u043e\u0442 \u043a\u043e\u0434:<\/p>\n<pre><code class=\"cpp\">\/\/ \u043a\u043e\u0434, \u0432\u044b\u0437\u044b\u0432\u0430\u044e\u0449\u0438\u0439 \u043d\u0430\u0448\u0443 \u0444\u0443\u043d\u043a\u0446\u0438\u044e auto t = str(\"10\"s);<\/code><\/pre>\n<p>\u0412 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435 \u043c\u044b \u0434\u043e\u043b\u0436\u043d\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0447\u0442\u043e-\u0442\u043e \u0432\u0440\u043e\u0434\u0435 \u044d\u0442\u043e\u0433\u043e:<\/p>\n<pre><code class=\"cpp\">In instantiation of 'std::__cxx11::string str(T) [with T =  std::__cxx11::basic_string&lt;char>; std::__cxx11::string =  std::__cxx11::basic_string&lt;char>]': required from here error: no matching function for call to  'to_string(std::__cxx11::basic_string&lt;char>&amp;)'     return std::to_string(t);<\/code><\/pre>\n<p><code>is_convertible<\/code>\u00a0 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 <code>true<\/code>\u00a0 \u0434\u043b\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u043e\u0433\u043e \u043d\u0430\u043c\u0438 \u0442\u0438\u043f\u0430 (<code>std::string<\/code>), \u0442\u0430\u043a \u0447\u0442\u043e \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043f\u0440\u043e\u0441\u0442\u043e \u0432\u0435\u0440\u043d\u0443\u0442\u044c <code>t<\/code>\u00a0 \u0431\u0435\u0437 \u043a\u0430\u043a\u043e\u0433\u043e-\u043b\u0438\u0431\u043e \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u044f\u2026 \u0442\u0430\u043a \u0447\u0442\u043e \u0436\u0435 \u043d\u0435 \u0442\u0430\u043a?<\/p>\n<p>\u0412\u043e\u0442 \u0432 \u0447\u0435\u043c \u0434\u0435\u043b\u043e:<\/p>\n<p>\u041a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 \u0441\u043a\u043e\u043c\u043f\u0438\u043b\u0438\u0440\u043e\u0432\u0430\u043b \u0432\u0441\u0435 \u0432\u0435\u0442\u043a\u0438 \u0438 \u043d\u0430\u0448\u0435\u043b \u043e\u0448\u0438\u0431\u043a\u0443 \u0432 <code>else<\/code>. \u041e\u043d \u043d\u0435 \u043c\u043e\u0436\u0435\u0442 \u043e\u0442\u0431\u0440\u043e\u0441\u0438\u0442\u044c \u201c\u043d\u0435\u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u044b\u0439\u201d \u043a\u043e\u0434 \u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0447\u0430\u0441\u0442\u043d\u043e\u0433\u043e \u0441\u043b\u0443\u0447\u0430\u044f \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u0438\u0437\u0430\u0446\u0438\u0438 \u0448\u0430\u0431\u043b\u043e\u043d\u0430.<\/p>\n<p>\u0412\u043e\u0442 \u0437\u0430\u0447\u0435\u043c \u043d\u0430\u043c \u043d\u0443\u0436\u0435\u043d \u0441\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0439 <code>if<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u201c\u043e\u0442\u0431\u0440\u043e\u0441\u0438\u0442\u201d \u043d\u0435\u043d\u0443\u0436\u043d\u044b\u0439 \u043a\u043e\u0434 \u0438 \u0441\u043a\u043e\u043c\u043f\u0438\u043b\u0438\u0440\u0443\u0435\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u0431\u043b\u043e\u043a, \u0432 \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u0435\u0434\u0435\u0442 \u0432\u0435\u0442\u0432\u043b\u0435\u043d\u0438\u0435. \u0418\u043d\u044b\u043c\u0438 \u0441\u043b\u043e\u0432\u0430\u043c\u0438, \u043c\u044b \u043f\u043e \u043f\u0440\u0435\u0436\u043d\u0435\u043c\u0443 \u0431\u0443\u0434\u0435\u043c \u0434\u0435\u043b\u0430\u0442\u044c \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0443 \u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0441\u0430 \u0434\u043b\u044f \u0432\u0441\u0435\u0433\u043e \u043a\u043e\u0434\u0430, \u043d\u043e \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0447\u0430\u0441\u0442\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043d\u0435 \u0431\u0443\u0434\u0443\u0442 \u0441\u043e\u0437\u0434\u0430\u043d\u044b.<\/p>\n<h3>std::enable_if \u00a0\u00a0<\/h3>\n<p>\u041e\u0434\u0438\u043d \u0438\u0437 \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u043c \u043c\u043e\u0436\u043d\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0439 if \u0432 C++11\/14 \u2014 \u044d\u0442\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c <code>enable_if<\/code>.<\/p>\n<p><code>enable_if<\/code> (\u0438 <code>enable_if_v<\/code> C++14). \u0423 \u043d\u0435\u0433\u043e \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u0441\u0442\u0440\u0430\u043d\u043d\u044b\u0439 \u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0441:<\/p>\n<pre><code class=\"cpp\">template&lt; bool B, class T = void >   struct enable_if;<\/code><\/pre>\n<p><code>enable_if<\/code> \u0432\u044b\u0432\u043e\u0434\u0438\u0442 \u0442\u0438\u043f T, \u0435\u0441\u043b\u0438 \u0432\u0445\u043e\u0434\u043d\u043e\u0435 \u0443\u0441\u043b\u043e\u0432\u0438\u0435 B \u0438\u0441\u0442\u0438\u043d\u043d\u043e. \u0412 \u043f\u0440\u043e\u0442\u0438\u0432\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u044d\u0442\u043e <a href=\"https:\/\/www.cppstories.com\/2016\/02\/notes-on-c-sfinae.html\">SFINAE<\/a>, \u0438 \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u0430\u044f \u043f\u0435\u0440\u0435\u0433\u0440\u0443\u0437\u043a\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0443\u0434\u0430\u043b\u044f\u0435\u0442\u0441\u044f \u0438\u0437 \u043d\u0430\u0431\u043e\u0440\u0430 \u043f\u0435\u0440\u0435\u0433\u0440\u0443\u0437\u043e\u043a. \u042d\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e \u0432 \u0441\u043b\u0443\u0447\u0430\u0435 false \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 \u201c\u043e\u0442\u0431\u0440\u0430\u0441\u044b\u0432\u0430\u0435\u0442\u201d \u043a\u043e\u0434 \u2014 \u044d\u0442\u043e \u043a\u0430\u043a \u0440\u0430\u0437 \u0442\u043e, \u0447\u0442\u043e \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e.<\/p>\n<p>\u041c\u044b \u043c\u043e\u0436\u0435\u043c \u043f\u0435\u0440\u0435\u043f\u0438\u0441\u0430\u0442\u044c \u043d\u0430\u0448 \u043f\u0440\u0438\u043c\u0435\u0440 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:<\/p>\n<pre><code class=\"cpp\">template &lt;typename T> enable_if_t&lt;is_convertible_v&lt;T, string>, string> strOld(T t) {     return t; }  template &lt;typename T> enable_if_t&lt;!is_convertible_v&lt;T, string>, string> strOld(T t) {     return to_string(t); } \/\/ \u043f\u0440\u0435\u0444\u0438\u043a\u0441 std:: \u0431\u044b\u043b \u043e\u043f\u0443\u0449\u0435\u043d<\/code><\/pre>\n<p>\u041d\u0435 \u0442\u0430\u043a \u043b\u0435\u0433\u043a\u043e\u2026 \u0432\u0435\u0440\u043d\u043e? \u041a\u0440\u043e\u043c\u0435 \u0442\u043e\u0433\u043e, \u044d\u0442\u0430 \u0432\u0435\u0440\u0441\u0438\u044f \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u043d\u0430\u043c\u043d\u043e\u0433\u043e \u0441\u043b\u043e\u0436\u043d\u0435\u0435, \u0447\u0435\u043c \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0438 \u043e\u0431\u044b\u0447\u043d\u0430\u044f \u043f\u0435\u0440\u0435\u0433\u0440\u0443\u0437\u043a\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u0439, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0431\u044b\u043b\u0438 \u0443 \u043d\u0430\u0441 \u0432 \u0441\u0430\u043c\u043e\u043c \u043d\u0430\u0447\u0430\u043b\u0435.<\/p>\n<p>\u0412\u043e\u0442 \u043f\u043e\u0447\u0435\u043c\u0443 \u043d\u0430\u043c \u043d\u0443\u0436\u0435\u043d <code>if constexpr<\/code> \u0438\u0437 C++17, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u043e\u0436\u0435\u0442 \u043f\u043e\u043c\u043e\u0447\u044c \u0432 \u0442\u0430\u043a\u0438\u0445 \u0441\u043b\u0443\u0447\u0430\u044f\u0445.<\/p>\n<p>\u041f\u043e\u0447\u0438\u0442\u0430\u0432 \u044d\u0442\u0443 \u0441\u0442\u0430\u0442\u044c\u044e, \u0432\u044b \u0441\u0430\u043c\u0438 \u0441\u043c\u043e\u0436\u0435\u0442\u0435 \u0431\u044b\u0441\u0442\u0440\u043e \u043f\u0435\u0440\u0435\u043f\u0438\u0441\u0430\u0442\u044c \u043d\u0430\u0448\u0443 \u0444\u0443\u043d\u043a\u0446\u0438\u044e <code>str<\/code> (\u0438\u043b\u0438 \u043d\u0430\u0439\u0442\u0438 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0432 \u043a\u043e\u043d\u0446\u0435).<\/p>\n<p>\u0427\u0442\u043e\u0431\u044b \u043b\u0443\u0447\u0448\u0435 \u0440\u0430\u0437\u043e\u0431\u0440\u0430\u0442\u044c\u0441\u044f \u0441 \u043d\u043e\u0432\u044b\u043c\u0438 \u0444\u0438\u0447\u0430\u043c\u0438, \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0431\u0430\u0437\u043e\u0432\u044b\u0445 \u043f\u0440\u0438\u043c\u0435\u0440\u043e\u0432:<\/p>\n<h3>\u041f\u0440\u0438\u043c\u0435\u0440 1 \u2014 \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u0435 \u0447\u0438\u0441\u0435\u043b\u00a0 \u00a0\u00a0<\/h3>\n<p>\u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u043d\u0430\u0447\u043d\u0435\u043c \u0441 \u0441\u0430\u043c\u043e\u0433\u043e \u043f\u0440\u043e\u0441\u0442\u043e\u0433\u043e \u043f\u0440\u0438\u043c\u0435\u0440\u0430: \u0444\u0443\u043d\u043a\u0446\u0438\u044f <code>close_enough<\/code>, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0441 \u0434\u0432\u0443\u043c\u044f \u0447\u0438\u0441\u043b\u0430\u043c\u0438. \u0415\u0441\u043b\u0438 \u0447\u0438\u0441\u043b\u0430 \u043d\u0435 \u044f\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u0447\u0438\u0441\u043b\u0430\u043c\u0438 \u0441 \u043f\u043b\u0430\u0432\u0430\u044e\u0449\u0435\u0439 \u0437\u0430\u043f\u044f\u0442\u043e\u0439 (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043a\u043e\u0433\u0434\u0430 \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0434\u0432\u0430 <code>int<\/code>), \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0441\u0440\u0430\u0432\u043d\u0438\u0442\u044c \u0438\u0445 \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e. \u0414\u043b\u044f \u0447\u0438\u0441\u0435\u043b \u0441 \u043f\u043b\u0430\u0432\u0430\u044e\u0449\u0435\u0439 \u0437\u0430\u043f\u044f\u0442\u043e\u0439 \u043b\u0443\u0447\u0448\u0435 \u0431\u0443\u0434\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u0443\u044e \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u043c\u0430\u043b\u0435\u043d\u044c\u043a\u0443\u044e \u0432\u0435\u043b\u0438\u0447\u0438\u043d\u0443, \u0441 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u0441\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u0442\u044c \u0438\u0445 \u0440\u0430\u0437\u043d\u0438\u0446\u0443 <code>abs &lt; epsilon<\/code>.<\/p>\n<p>\u042f \u043d\u0430\u0448\u0435\u043b \u044d\u0442\u043e\u0442 \u043a\u043e\u0434 \u0432 <a href=\"https:\/\/h-deb.clg.qc.ca\/WG21\/PracticalCpp\/Temperature.html\">Practical Modern C++ Teaser<\/a> \u2014 \u0444\u0430\u043d\u0442\u0430\u0441\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u043c \u043f\u043e\u0448\u0430\u0433\u043e\u0432\u043e\u043c \u0440\u0443\u043a\u043e\u0432\u043e\u0434\u0441\u0442\u0432\u0435 \u043f\u043e \u0441\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u043c \u0444\u0438\u0447\u0430\u043c C++, \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u043d\u043e\u043c \u041f\u0430\u0442\u0440\u0438\u0441\u043e\u043c \u0420\u043e\u0435\u043c (<a href=\"https:\/\/twitter.com\/PatriceRoy1\">Patrice Roy<\/a>). \u041e\u043d \u0431\u044b\u043b \u043e\u0447\u0435\u043d\u044c \u043b\u044e\u0431\u0435\u0437\u0435\u043d \u0438 \u043f\u043e\u0437\u0432\u043e\u043b\u0438\u043b \u043c\u043d\u0435 \u0432\u043a\u043b\u044e\u0447\u0438\u0442\u044c \u044d\u0442\u043e\u0442 \u043f\u0440\u0438\u043c\u0435\u0440 \u0432 \u0441\u0432\u043e\u044e \u0441\u0442\u0430\u0442\u044c\u044e.<\/p>\n<p>\u0412\u0435\u0440\u0441\u0438\u044f \u0421++11\/14:<\/p>\n<pre><code class=\"cpp\">template &lt;class T> constexpr T absolute(T arg) {    return arg &lt; 0 ? -arg : arg; }  template &lt;class T>  constexpr enable_if_t&lt;is_floating_point&lt;T>::value, bool>  close_enough(T a, T b) {    return absolute(a - b) &lt; static_cast&lt;T>(0.000001); } template &lt;class T> constexpr enable_if_t&lt;!is_floating_point&lt;T>::value, bool>  close_enough(T a, T b) {    return a == b; }<\/code><\/pre>\n<p>\u041a\u0430\u043a \u0432\u0438\u0434\u0438\u0442\u0435, \u0437\u0434\u0435\u0441\u044c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f <code>enable_if<\/code>. \u042d\u0442\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u043e\u0447\u0435\u043d\u044c \u043f\u043e\u0445\u043e\u0436\u0430 \u043d\u0430 \u043d\u0430\u0448\u0443 \u0444\u0443\u043d\u043a\u0446\u0438\u044e <code>str<\/code>. \u041a\u043e\u0434 \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u0442 \u0442\u0438\u043f \u0432\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u0447\u0438\u0441\u043b\u0430 \u2014 <code>is_floating_point<\/code>. \u0417\u0430\u0442\u0435\u043c \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 \u043c\u043e\u0436\u0435\u0442 \u0443\u0434\u0430\u043b\u0438\u0442\u044c \u043e\u0434\u043d\u0443 \u0438\u0437 \u043f\u0435\u0440\u0435\u0433\u0440\u0443\u0437\u043e\u043a \u0444\u0443\u043d\u043a\u0446\u0438\u0438.<\/p>\n<p>\u0410 \u0442\u0435\u043f\u0435\u0440\u044c \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043d\u0430 \u0432\u0435\u0440\u0441\u0438\u044e C++17:<\/p>\n<pre><code class=\"cpp\">template &lt;class T> constexpr T absolute(T arg) {    return arg &lt; 0 ? -arg : arg; }  template &lt;class T> constexpr auto precision_threshold = T(0.000001);  template &lt;class T> constexpr bool close_enough(T a, T b) {    if constexpr (is_floating_point_v&lt;T>) \/\/ &lt;&lt; !!       return absolute(a - b) &lt; precision_threshold&lt;T>;    else       return a == b; }<\/code><\/pre>\n<p>\u0412\u0430\u0443\u2026 \u0432\u0441\u0435 \u0437\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u043e \u0432 \u043e\u0434\u043d\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043a \u0442\u043e\u043c\u0443 \u0436\u0435 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u043f\u043e\u0447\u0442\u0438 \u043a\u0430\u043a \u043e\u0431\u044b\u0447\u043d\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f.<\/p>\n<p>\u0421 \u043f\u043e\u0447\u0442\u0438 \u201c\u0437\u0430\u0443\u0440\u044f\u0434\u043d\u044b\u043c\u201d <code>if<\/code> \ud83d\ude42<\/p>\n<p>\u0412 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u0438 <code>if constexpr constexpr<\/code> \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u0432\u044b\u0447\u0438\u0441\u043b\u044f\u0435\u0442\u0441\u044f \u0432\u043e \u0432\u0440\u0435\u043c\u044f \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0446\u0438\u0438, \u043f\u043e\u0441\u043b\u0435 \u0447\u0435\u0433\u043e \u043a\u043e\u0434 \u0432 \u043e\u0434\u043d\u043e\u0439 \u0438\u0437 \u0432\u0435\u0442\u0432\u0435\u0439 \u043e\u0442\u0431\u0440\u0430\u0441\u044b\u0432\u0430\u0435\u0442\u0441\u044f.<\/p>\n<p>\u041d\u043e \u0432\u0430\u0436\u043d\u043e \u043e\u0442\u043c\u0435\u0442\u0438\u0442\u044c, \u0447\u0442\u043e \u043e\u0442\u0431\u0440\u043e\u0448\u0435\u043d\u043d\u044b\u0439 \u043a\u043e\u0434 \u0432\u0441\u0435-\u0442\u0430\u043a\u0438 \u0434\u043e\u043b\u0436\u0435\u043d \u0438\u043c\u0435\u0442\u044c \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u044b\u0439 \u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0441. \u041a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442 \u0431\u0430\u0437\u043e\u0432\u0443\u044e \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0443 \u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0441\u0430, \u043d\u043e \u0437\u0430\u0442\u0435\u043c \u043f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u0442 \u044d\u0442\u0443 \u0447\u0430\u0441\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043d\u0430 \u044d\u0442\u0430\u043f\u0435 \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u0438\u0437\u0430\u0446\u0438\u0438 \u0448\u0430\u0431\u043b\u043e\u043d\u0430.<\/p>\n<p>\u0418\u043c\u0435\u043d\u043d\u043e \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u043a\u043e\u0434 \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u0442 \u043e\u0448\u0438\u0431\u043a\u0443 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440\u0430:<\/p>\n<pre><code class=\"cpp\">template &lt;class T> constexpr bool close_enough(T a, T b) {    if constexpr (is_floating_point_v&lt;T>)        return absolute(a - b) &lt; precision_threshold&lt;T>;    else       return aaaa == bxxxx; \/\/ \u043e\u0448\u0438\u0431\u043a\u0430 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440\u0430 - \u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0441! }  close_enough(10.04f, 20.f);<\/code><\/pre>\n<p>\u041a\u0441\u0442\u0430\u0442\u0438, \u0437\u0430\u043c\u0435\u0442\u0438\u043b\u0438 \u043b\u0438 \u0432\u044b \u043a\u0430\u043a\u0438\u0435-\u043d\u0438\u0431\u0443\u0434\u044c \u0434\u0440\u0443\u0433\u0438\u0435 \u0444\u0438\u0447\u0438 C++17, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0437\u0434\u0435\u0441\u044c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b\u0438\u0441\u044c?<\/p>\n<p>\u0412\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u044d\u0442\u043e\u0442 \u043a\u043e\u0434 \u0432 <a href=\"https:\/\/godbolt.org\/z\/KbaahbP1x\">@Compiler Explorer<\/a><\/p>\n<h4>\u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u043a\u043e\u043d\u0446\u0435\u043f\u0442\u043e\u0432 C++20 \u00a0\u00a0<\/h4>\n<p>\u041d\u043e \u043f\u043e\u0434\u043e\u0436\u0434\u0438\u0442\u0435\u2026 \u043d\u0430 \u0434\u0432\u043e\u0440\u0435 \u0443\u0436\u0435 2021 \u0433\u043e\u0434, \u0442\u0430\u043a \u043f\u043e\u0447\u0435\u043c\u0443 \u0431\u044b \u043d\u0430\u043c \u043d\u0435 \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u043a\u043e\u043d\u0446\u0435\u043f\u0442\u0430\u043c\u0438? \ud83d\ude42<\/p>\n<p>\u0414\u043e C++20 \u043c\u044b \u043c\u043e\u0433\u043b\u0438 \u0440\u0430\u0441\u0441\u043c\u0430\u0442\u0440\u0438\u0432\u0430\u0442\u044c \u0448\u0430\u0431\u043b\u043e\u043d\u043d\u044b\u0435 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b \u043a\u0430\u043a \u0447\u0442\u043e-\u0442\u043e \u0432\u0440\u043e\u0434\u0435 void* \u0432 \u043e\u0431\u044b\u0447\u043d\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438. \u0415\u0441\u043b\u0438 \u0432\u044b \u0445\u043e\u0442\u0435\u043b\u0438 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0438\u0442\u044c \u0442\u0430\u043a\u043e\u0439 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440, \u0432\u0430\u043c \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u043b\u043e\u0441\u044c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0435 \u043c\u0435\u0442\u043e\u0434\u044b, \u043e\u043f\u0438\u0441\u0430\u043d\u043d\u044b\u0435 \u0432 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435. \u041d\u043e \u0432\u043c\u0435\u0441\u0442\u0435 \u0441 \u043a\u043e\u043d\u0446\u0435\u043f\u0442\u0430\u043c\u0438 \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u0438 \u0435\u0441\u0442\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0439 \u0441\u043f\u043e\u0441\u043e\u0431 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0438\u0442\u044c \u044d\u0442\u0438 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b.<\/p>\n<p>\u0412\u0437\u0433\u043b\u044f\u043d\u0438\u0442\u0435 \u043d\u0430 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442 \u043a\u043e\u0434\u0430:\u00a0<\/p>\n<pre><code class=\"cpp\">template &lt;typename T> requires std::is_floating_point_v&lt;T> constexpr bool close_enough20(T a, T b) {    return absolute(a - b) &lt; precision_threshold&lt;T>; } constexpr bool close_enough20(auto a, auto b) {    return a == b; }<\/code><\/pre>\n<p>\u041a\u0430\u043a \u0432\u0438\u0434\u0438\u0442\u0435, \u0432\u0435\u0440\u0441\u0438\u044f C++20 \u0432\u0435\u0440\u043d\u0443\u043b\u0430\u0441\u044c \u043e\u0431\u0440\u0430\u0442\u043d\u043e \u043a \u0434\u0432\u0443\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u044f\u043c. \u041d\u043e \u0442\u0435\u043f\u0435\u0440\u044c \u043a\u043e\u0434 \u043d\u0430\u043c\u043d\u043e\u0433\u043e \u0447\u0438\u0442\u0430\u0431\u0435\u043b\u044c\u043d\u0435\u0435, \u0447\u0435\u043c \u0441 <code>enable_if<\/code>. \u0421 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043a\u043e\u043d\u0446\u0435\u043f\u0442\u043e\u0432 \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043b\u0435\u0433\u043a\u043e \u0432\u044b\u0440\u0430\u0437\u0438\u0442\u044c \u043d\u0430\u0448\u0438 \u0442\u0440\u0435\u0431\u043e\u0432\u0430\u043d\u0438\u044f \u043a \u0448\u0430\u0431\u043b\u043e\u043d\u043d\u044b\u043c \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430\u043c:<\/p>\n<pre><code class=\"cpp\">requires std::is_floating_point_v&lt;T><\/code><\/pre>\n<p><code>is_floating_point_v<\/code> \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u043e\u043c \u0442\u0438\u043f\u0430 (\u0438\u0437 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 <code>&lt;type_traits><\/code>), \u0430 \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440 <code>requires<\/code>, \u043a\u0430\u043a \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u0432\u0438\u0434\u0435\u0442\u044c, \u0432\u044b\u0447\u0438\u0441\u043b\u044f\u0435\u0442 \u0431\u0443\u043b\u0435\u0432\u044b \u043a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u043d\u044b\u0435 \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u044f.<\/p>\n<p>\u0412\u0442\u043e\u0440\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u043d\u043e\u0432\u044b\u0439 \u043e\u0431\u043e\u0431\u0449\u0435\u043d\u043d\u044b\u0439 \u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0441 \u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c <code>template&lt;><\/code> \u0438 \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c:<\/p>\n<pre><code class=\"cpp\">constexpr bool close_enough20(auto a, auto b) { }<\/code><\/pre>\n<p>\u0422\u0430\u043a\u043e\u0439 \u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0441 \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u0438 \u0441 <a href=\"https:\/\/www.cppstories.com\/2020\/08\/lambda-generic.html\/\">\u043e\u0431\u043e\u0431\u0449\u0435\u043d\u043d\u044b\u043c\u0438 (generic) \u043b\u044f\u043c\u0431\u0434\u0430-\u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u044f\u043c\u0438<\/a>. \u042d\u0442\u043e \u043d\u0435 \u043f\u0440\u044f\u043c\u0430\u044f \u0442\u0440\u0430\u043d\u0441\u043b\u044f\u0446\u0438\u044f \u043d\u0430\u0448\u0435\u0433\u043e C++11\/14 \u043a\u043e\u0434\u0430, \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u043e\u043d \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u0435\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439 \u0441\u0438\u0433\u043d\u0430\u0442\u0443\u0440\u0435:<\/p>\n<pre><code class=\"cpp\">template &lt;typename T, typename U> constexpr bool close_enough20(T a, U b) { }<\/code><\/pre>\n<p>\u0412\u0434\u043e\u0431\u0430\u0432\u043e\u043a, C++20 \u043f\u0440\u0435\u0434\u043b\u0430\u0433\u0430\u0435\u0442 \u043d\u0430\u043c \u043a\u0440\u0430\u0442\u043a\u0438\u0439 \u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0441 \u0434\u043b\u044f \u043a\u043e\u043d\u0446\u0435\u043f\u0442\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043e\u0441\u043d\u043e\u0432\u0430\u043d \u043d\u0430 auto \u0441 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f\u043c\u0438 (constrained auto):<\/p>\n<pre><code class=\"cpp\">constexpr bool close_enough20(std::floating_point auto a,                               std::floating_point auto b) {    return absolute(a - b) &lt; precision_threshold&lt;std::common_type_t&lt;decltype(a), decltype(b)>>; } constexpr bool close_enough20(std::integral auto a, std::integral auto b) {    return a == b; }<\/code><\/pre>\n<p>\u0412 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0430\u043b\u044c\u0442\u0435\u0440\u043d\u0430\u0442\u0438\u0432\u044b, \u043c\u044b \u0442\u0430\u043a\u0436\u0435 \u043c\u043e\u0436\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0438\u043c\u044f \u043a\u043e\u043d\u0446\u0435\u043f\u0442\u0430 \u0432\u043c\u0435\u0441\u0442\u043e <code>typename<\/code> \u0431\u0435\u0437 \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u0430 <code>requires<\/code>:<\/p>\n<pre><code class=\"cpp\">template &lt;std::is_floating_point T> constexpr bool close_enough20(T a, T b) {    return absolute(a - b) &lt; precision_threshold&lt;T)>; }<\/code><\/pre>\n<p>\u0412 \u044d\u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043c\u044b \u0442\u0430\u043a\u0436\u0435 \u043f\u0435\u0440\u0435\u043a\u043b\u044e\u0447\u0438\u043b\u0438\u0441\u044c \u0441 <code>is_floating_point_v<\/code> \u043d\u0430 \u043a\u043e\u043d\u0446\u0435\u043f\u0442 <code>floating_point<\/code> \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u044b\u0439 \u043f\u043e\u0434 \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043a\u043e\u043c <code>&lt;concepts><\/code>.<\/p>\n<p>\u0412\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u044d\u0442\u043e\u0442 \u043a\u043e\u0434 \u0432 <a href=\"https:\/\/godbolt.org\/z\/x5c3rdn1W\">@Compiler Explorer<\/a><\/p>\n<p>\u0425\u043e\u0440\u043e\u0448\u043e, \u0430 \u043a\u0430\u043a \u043d\u0430\u0441\u0447\u0435\u0442 \u0434\u0440\u0443\u0433\u0438\u0445 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f?<\/p>\n<h3>\u041f\u0440\u0438\u043c\u0435\u0440 2 \u2014 \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0435 \u0441\u0440\u0435\u0434\u043d\u0435\u0433\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f\u00a0<\/h3>\n<p>\u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0435\u0449\u0435 \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u043f\u043e\u0440\u0430\u0431\u043e\u0442\u0430\u0435\u043c \u0441 \u0447\u0438\u0441\u043b\u0430\u043c\u0438. \u0422\u0435\u043f\u0435\u0440\u044c \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u043f\u0438\u0441\u0430\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u044e, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0431\u0435\u0440\u0435\u0442 \u043d\u0430 \u0432\u0445\u043e\u0434 \u0447\u0438\u0441\u043b\u043e\u0432\u043e\u0439 \u0432\u0435\u043a\u0442\u043e\u0440 \u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0441\u0440\u0435\u0434\u043d\u0435\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435.<\/p>\n<p>\u0412\u043e\u0442, \u0447\u0442\u043e \u043c\u044b \u0445\u043e\u0442\u0435\u043b\u0438 \u0431\u044b \u0432\u0438\u0434\u0435\u0442\u044c:<\/p>\n<pre><code class=\"cpp\">std::vector ints { 1, 2, 3, 4, 5}; std::cout &lt;&lt; Average(ints) &lt;&lt; '\\n';<\/code><\/pre>\n<p>\u041d\u0430\u0448\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0434\u043e\u043b\u0436\u043d\u0430:<\/p>\n<ul>\n<li>\n<p>\u041f\u0440\u0438\u043d\u0438\u043c\u0430\u0442\u044c \u0447\u0438\u0441\u043b\u0430 \u0441 \u043f\u043b\u0430\u0432\u0430\u044e\u0449\u0435\u0439 \u0437\u0430\u043f\u044f\u0442\u043e\u0439 \u0438\u043b\u0438 \u0446\u0435\u043b\u043e\u0447\u0438\u0441\u043b\u0435\u043d\u043d\u044b\u0435 \u0442\u0438\u043f\u044b.<\/p>\n<\/li>\n<li>\n<p>\u0412\u043e<\/p>\n<\/li>\n<\/ul>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\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-338446","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/338446","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=338446"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/338446\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=338446"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=338446"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=338446"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}