{"id":555031,"date":"2022-02-17T04:21:32","date_gmt":"2022-02-16T20:21:32","guid":{"rendered":"http:\/\/4563.org\/?p=555031"},"modified":"2022-02-17T04:21:32","modified_gmt":"2022-02-16T20:21:32","slug":"c-%e6%a8%a1%e7%89%88%e7%bc%96%e8%af%91%e9%97%ae%e9%a2%98-func-bool-if-constexpr-a-else-b-%e5%92%8c%e7%9b%b4%e6%8e%a5%e5%a3%b0%e6%98%8e-static-constexpr-bool-%e7%bc%96%e8%af%91%e7%9a%84-4","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=555031","title":{"rendered":"c++ \u6a21\u7248\u7f16\u8bd1\u95ee\u9898, func -&gt; bool { if constexpr A else B } \u548c\u76f4\u63a5\u58f0\u660e static constexpr bool \u7f16\u8bd1\u7684\u533a\u522b"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1> c++ \u6a21\u7248\u7f16\u8bd1\u95ee\u9898, func -&gt; bool { if constexpr A else B } \u548c\u76f4\u63a5\u58f0\u660e static constexpr bool \u7f16\u8bd1\u7684\u533a\u522b <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : sl0000 <\/span>  <span><i><\/i> 35<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<pre><code>struct is {     template&lt;typename T&gt;     static constexpr bool _vector() {         if constexpr (std::is_fundamental_v&lt;T&gt;) {             return false;         } else {             return std::is_same_v&lt;T, std::vector&lt;typename T::value_type&gt;&gt;;         }     }     template&lt;typename T&gt;     static constexpr bool vector = _vector&lt;T&gt;();          template&lt;typename T&gt;     static constexpr bool vector2 = std::is_fundamental_v&lt;T&gt; || (!std::is_fundamental_v&lt;T&gt; &amp;&amp; std::is_same_v&lt;T, std::vector&lt;typename T::value_type&gt;&gt;); };    int main(int argc, const char * argv[]) {     std::cout &lt;&lt; is::vector2&lt;std::vector&lt;std::vector&lt;int&gt;&gt;::value_type::value_type&gt; &lt;&lt; std::endl;     return 0; } <\/code><\/pre>\n<p>\u4f7f\u7528 vector \u662f ok \u7684<\/p>\n<p>\u4f7f\u7528 vector2 \u7f16\u8bd1\u4f1a\u62a5 Type &#8216;int&#8217; cannot be used prior to &#8216;::&#8217; because it has no members<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>10<\/span>) <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"7059718\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : q2577 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>  <\/span> <\/div>\n<\/p><\/div>\n<div> &#8220;`<br \/>template&lt;class T&gt;<br \/>struct is_vector {<br \/> static constexpr bool value = false;<br \/>};<br \/>template&lt;class T&gt;<br \/>struct is_vector&lt;std::vector&lt;T&gt; &gt; {<br \/> static constexpr bool value = true;<br \/>};<br \/>&#8220;&#8220; <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"7059719\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : sl0000 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>  <\/span> <\/div>\n<\/p><\/div>\n<div> @q2577 \u5c45\u7136\u8fd9\u4e48\u7b80\u5355\u3002\u3002\u3002\u4f46\u662f \u4e0a\u9762\u7684\u7f16\u8bd1\u95ee\u9898 \u80fd\u89e3\u91ca\u4e00\u4e0b\u5417 <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"7059720\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : q2577 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>  <\/span> <\/div>\n<\/p><\/div>\n<div> @sl0000 \u53ef\u80fd\u662f\u7f16\u8bd1\u5668\u95ee\u9898\uff1f <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"7059721\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : L4Linux <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>  <\/span> <\/div>\n<\/p><\/div>\n<div> vector2 \u903b\u8f91\u64cd\u4f5c\u5de6\u53f3\u4e24\u8fb9\u5728\u7f16\u8bd1\u65f6\u90fd\u4f1a\u6c42\u503c\uff0c\u6240\u4ee5\u51fa\u73b0\u4e86 int::value_type \u3002 <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"7059722\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : GeruzoniAnsasu <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>  <\/span> <\/div>\n<\/p><\/div>\n<div> @L4Linux \u4e0d\u80fd\u53eb\u300c\u5de6\u53f3\u4e24\u8fb9\u90fd\u6c42\u503c\u300d\u5427\uff0c\u6211\u89c9\u5f97\u5e94\u8be5\u53eb\u300c\u6a21\u677f\u66ff\u6362\u4e0d\u53d7\u8868\u8fbe\u5f0f\u672c\u8eab\u7684\u903b\u8f91\u5f71\u54cd\u300d\uff0c\u5c31\uff0c\u8fd9\u4e2a\u8868\u8fbe\u5f0f\u672c\u8eab\u7684\u903b\u8f91\u6210\u4e0d\u6210\u7acb\u5e76\u4e0d\u5f71\u54cd\u7528 int \u53bb\u66ff\u6362 T \uff0c\u8fd9\u4e2a\u8fc7\u7a0b\u4e5f\u5e76\u6ca1\u6709\u89e6\u53d1 SFINAE \uff0c\u6240\u4ee5\u66ff\u6362\u5931\u8d25\uff0c\u7f16\u8bd1\u62a5\u9519<br \/>@sl0000 \u90a3\u4e48\u5982\u679c\u7528 SFINAE \u6765\u5bb9\u7eb3\u66ff\u6362\u9519\u8bef\u7684\u8868\u8fbe\u5f0f\uff0c\u5c31\u53ef\u4ee5\u5de5\u4f5c\u4e86\uff1a<br \/>https:\/\/gcc.godbolt.org\/z\/eE5Gar4z3 <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"7059723\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : edimetia3d <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>  <\/span> <\/div>\n<\/p><\/div>\n<div> \u867d\u7136\u5f88\u4e0d\u60f3\u56de, \u4f46\u662f\u65e2\u7136\u90fd\u770b\u4e86. \u8fd8\u662f\u56de\u590d\u4e00\u4e0b\u5427.<br \/>1. \u9ebb\u70e6\u95ee\u95ee\u9898\u524d\u628a\u4ee3\u7801\u6e05\u7406\u4e00\u4e0b, \u6574\u7406\u4e00\u4e2a\u6700\u7b80\u5355\u7684 case \u51fa\u6765.<br \/>2. @L4Linux \u6b63\u89e3<br \/>3. \u4f60\u628a `if constexpr` \u6362\u6210 `if`\u4e5f\u4f1a\u7f16\u8bd1\u51fa\u9519, \u4e00\u6837\u7684\u9053\u7406, \u81f3\u4e8e\u8fd9\u4e2a\u9053\u7406\u4f60\u61c2\u4e0d\u61c2, \u5c31\u770b\u4f60\u4e86. <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"7059724\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : test0x01 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>  <\/span> <\/div>\n<\/p><\/div>\n<div> \u6211\u53d1\u73b0\uff0c\u5341\u4e94\u5e74\u6ca1\u7528 c++, \u57fa\u672c\u4e0a\u770b\u4e0d\u61c2\u4e86 <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"7059725\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Caturra <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>  <\/span> <\/div>\n<\/p><\/div>\n<div> \u6cdb\u578b\u7f16\u7a0b\u4e2d\u8fd9\u79cd\u4f2a\u00b7\u77ed\u8def\u8fd0\u7b97\u662f\u4e0d\u7ba1\u7528\u7684\uff0c\u5b9e\u4f8b\u5316\u51fa\u6765\u7f16\u8bd1\u5668\u770b\u4e0d\u61c2\u5c31\u662f\u4e0d\u884c\uff08 int::value_type \uff09\uff0c<br \/>\u4f46\u662f\u4f20\u7edf\u4e0a\u53ef\u4ee5\u7528 SFINAE \u6216\u8005\u7b80\u5355\u70b9\u76f4\u63a5\u4e0a\u7279\u5316\u6765\u89e3\u51b3\u95ee\u9898<br \/>\u76f8\u6bd4\u4e4b\u4e0b if-constexpr \u53ef\u8bfb\u6027\u4e5f\u633a\u597d\uff0c\u4e0d\u7528\u5199\u4e00\u5806 struct \uff08\u6211\u4e5f\u60f3\u591a\u7528\u70b9 17 \u7684\u7279\u6027\u5509 <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"7059726\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : GeruzoniAnsasu <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>  <\/span> <\/div>\n<\/p><\/div>\n<div> @sl0000 <br \/>\u53d1\u73b0#5 \u7684\u4ee3\u7801\u5199\u9519\u4e86\uff0c\u6539\u6b63\u4e86\u4e00\u70b9\u7ec6\u8282\uff0c\u8fd9\u4e2a\u4f8b\u5b50\u53ef\u4ee5\u5de5\u4f5c<br \/>https:\/\/gcc.godbolt.org\/z\/h3nnch497 <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"7059727\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : GeruzoniAnsasu <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>  <\/span> <\/div>\n<\/p><\/div>\n<div> \u53e6\u5916\u8fd9\u4e2a constexpr if \u586b\u8865\u4e86\u6211\u4e00\u4e2a\u77e5\u8bc6\u7a7a\u767d\uff1a<br \/>constexpr \u4e5f\u6709\u7c7b\u4f3c\u4e24\u6bb5\u540d\u5b57\u67e5\u627e\u7684\u7279\u6027\uff0c\u5982\u679c\u5b9a\u4e49\u5728\u6a21\u677f\u4e2d\uff0c\u90a3\u4e48\u5426\u5b9a\u5206\u652f\u4e2d\u7684\u4ee3\u7801\u4f1a\u76f4\u63a5\u4e22\u5f03\uff1b\u5982\u679c\u5904\u4e8e\u6a21\u677f\u5b9e\u4f8b\u5316\u9636\u6bb5\uff0c\u5219\u4f1a\u6b63\u5e38\u66ff\u6362\u5206\u652f\u4e24\u4fa7\u7684\u6240\u6709\u9700\u8981\u5b9e\u4f8b\u5316\u7684\u6a21\u677f\u4ee3\u7801<br \/>https:\/\/stackoverflow.com\/questions\/50051473\/if-constexpr-in-c17-does-not-work-in-a-non-templated-function<br \/>\u6211\u4eec\u5438\u8279\u771f\u7684\u592a\u725b\u903c\u8fa3 <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>c++ \u6a21\u7248\u7f16\u8bd1\u95ee\u9898, func &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/555031"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=555031"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/555031\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=555031"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=555031"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=555031"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}