{"id":125388,"date":"2020-06-19T05:08:24","date_gmt":"2020-06-18T21:08:24","guid":{"rendered":"http:\/\/4563.org\/?p=125388"},"modified":"2020-06-19T05:08:24","modified_gmt":"2020-06-18T21:08:24","slug":"c-%e6%a8%a1%e6%9d%bf-sfinae-%e9%97%ae%e9%a2%98","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=125388","title":{"rendered":"C++ \u6a21\u677f SFINAE \u95ee\u9898"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  C++ \u6a21\u677f SFINAE \u95ee\u9898               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Tony042 <\/span>  <span><i><\/i> 9<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u5728\u8bfb&lt;&lt;C++ Templates&gt;&gt;\u7b2c\u4e8c\u7248\u65f6\uff0c\u6709\u4e00\u4e2a\u6a21\u677f\u95ee\u9898\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<p>\u6a21\u677f\u5b9a\u4e49\uff1a<\/p>\n<pre><code>#include &lt;utility&gt;  template &lt;typename F, typename... Args, typename = decltype(std::declval&lt;F&gt;()(std::declval&lt;Args &amp;&amp;&gt;()...))&gt; std::true_type isValidImpl(void *);  template &lt;typename F, typename... Args&gt; std::false_type isValidImpl(...);  inline constexpr auto isValid = [](auto f) {     return [](auto &amp;&amp;... args) {         return decltype(isValidImpl&lt;decltype(f), decltype(args) &amp;&amp;...&gt;(nullptr)){};     }; };  template &lt;typename T&gt; struct TypeT {     using Type = T; };  template &lt;typename T&gt; constexpr auto type = TypeT&lt;T&gt;{};  template &lt;typename T&gt; T valueT(TypeT&lt;T&gt;);  constexpr auto isDefaultConstructible = isValid([](auto x) -&gt; decltype((void)decltype(valueT(x))()) {});  <\/code><\/pre>\n<p>\u5982\u679c\u4e00\u4e2a lambda \u51fd\u6570\u4e0d\u6267\u884c\u662f\u4e0d\u662f\u5c31\u4e0d\u68c0\u67e5\u8fd4\u56de\u7c7b\u578b\u5408\u6cd5\u6027\u4e86\u5462,\u6bd4\u5982\u5728\u6267\u884c<code>isDefaultConstructible(type&lt;x&gt;)<\/code>\u547d\u4ee4\u65f6\uff0c\u4f1a\u5f15\u5165\u4e00\u4e2a lamda \u51fd\u6570\uff0c\u8fd9\u4e2a lambda \u51fd\u6570\u8fd4\u56de\u503c\u662f<code>decltype((void)decltype(valueT(x))()) {})<\/code>, <code>decltype(valueT(x))()<\/code>\u5728 x \u662f int \u7684\u65f6\u5019\u5408\u6cd5\uff0c\u5bfc\u81f4\u6700\u7ec8\u8c03\u7528\u7b2c\u4e00\u4e2a<code>isValidImpl<\/code>\u51fd\u6570\uff0cx \u662f int&amp;\u7684\u65f6\u5019\u4e0d\u5408\u6cd5\uff0c\u628a\u7b2c\u4e00\u4e2a<code>isValidImpl<\/code> SFINAE out\uff0c\u8c03\u7528\u7b2c\u4e8c\u4e2a\u6b64\u4f18\u5148\u7ea7<code>isValidImpl<\/code>\u51fd\u6570\uff0c\u5373\u4f7f isValid \u4f20\u5165\u7684\u662f\u4e00\u4e2a\u4e0d\u5408\u6cd5 lambda \u51fd\u6570\uff0c\u4f46\u662f\u7531\u4e8e\u6ca1\u6709\u4f7f\u7528\u6240\u4ee5\u6ca1\u6709\u68c0\u67e5 lambda \u51fd\u6570\u5408\u6cd5\u6027\uff0c\u6211\u7684\u7406\u89e3\u6b63\u786e\u5417\uff1f<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>7<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"2140523\" 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>                                                             \u8fd9\u3002\u3002 \u5199\u7684\u5565\u4e1c\u897f<\/p>\n<p>\u5f3a\u70c8\u5efa\u8bae\u4f60\u81ea\u5df1\u4e22\u5230 clion \u91cc\u53bb\u8c03\u4e00\u8c03\u8fd9\u662f\u5728\u5e72\u4ec0\u4e48<\/p>\n<p>\u9996\u5148\u83ab\u540d\u5176\u5999\u7684 inline \u662f\u54ea\u6765\u7684\uff1f<br \/>\u7136\u540e\u6700\u540e\u8fd9\u4e2a isDefaultConstructible \u662f\u5565\u610f\u601d\uff1f \u5b83\u662f\u4e00\u4e2a is_valid \u8fd4\u56de\u7684 lambda\uff0c\u63a5\u53d7\u4efb\u610f\u591a\u4e2a\u53c2\u6570<\/p>\n<p>\u6211\u89c9\u5f97\u8fd9\u5768\u9762\u6761\u53ef\u80fd\u8ddf\u4f60\u9884\u671f\u7684\u5b8c\u5168\u4e0d\u4e00\u6837                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2140524\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : Tony042 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @GeruzoniAnsasu \u8fd9\u4e2a\u662f\u53ef\u4ee5\u8dd1\u901a\u7684\uff0c\u53ea\u4e0d\u8fc7\u7528\u4e86 lambda \u95ed\u5305\uff0c\u9700\u8981 C++17 \u53ca\u4ee5\u4e0a\u6807\u51c6\uff0c\u5df2\u7ecf\u5728 GCC10 \u548c MSVC 16.9 \u90fd\u6d4b\u8bd5\u8fc7\u4e86\uff0c\u6ca1\u95ee\u9898\u7684\uff0cisDefaultConstructible \u662f\u4e00\u4e2a lamda \u51fd\u6570\uff0c\u7528\u6765\u8fd4\u56de\u7c7b\u578b\u662f\u5426\u6709\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\uff0c\u5982\u679c\u6709\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u5219\u8fd4\u56de std::true_type \u5426\u5219\u8fd4\u56de std::false_type, \u7528\u6cd5\u662f isConstructible(type&lt;int&gt;)\uff0c\u5f53\u7136\u4f60\u4e5f\u53ef\u4ee5\u518d\u5b9a\u4e49\u4e00\u4e2a\u7c7b\u578b\u6a21\u677f template &lt;typename T&gt;<br \/>using isDefaultConstructibleT = decltype(isDefaultConstructible(type&lt;T&gt;));                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2140525\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : Tony042 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @GeruzoniAnsasu \u7136\u540e\u8fd9\u4e2a\u662f C++ templates 2nd edition 19.4.3 \u7684\u4e00\u4e2a\u4f8b\u5b50\uff0c\u6211\u6709\u4e2a\u5730\u65b9\u4e0d\u592a\u7406\u89e3\uff0c\u5c31\u53d1\u4e0a\u6765\u4e00\u4e0b\uff0c\u81f3\u4e8e inline \u6211\u63a8\u6d4b\u4f5c\u8005\u653e\u5728\u8fd9\u7684\u4f4d\u7f6e\u662f\u4e3a\u4e86\u4f18\u5316\u6027\u80fd\uff0c\u4e0d\u8fc7 inline \u8fd9\u4e2a\u8fd8\u662f\u8981\u770b\u7f16\u8bd1\u5668\uff0c\u5e94\u8be5\u6ca1\u5f71\u54cd\u7684\uff0c\u8fd9\u4e2a\u6a21\u677f\u51fd\u6570\u770b\u8d77\u6765\u786e\u5b9e\u6bd4\u8f83\u552c\u4eba\uff0c\u4e0d\u8fc7\u4ed4\u7ec6\u770b\u5e94\u8be5\u8fd8\u662f\u53ef\u4ee5\u7406\u89e3\u7684                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2140526\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : Tony042 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @GeruzoniAnsasu \u4e0d\u597d\u610f\u601d\uff0c\u6211\u6ca1\u6709\u628a\u6761\u4ef6\u8bf4\u6e05\u6f88\uff0c\u662f\u6211\u7684\u9505                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2140527\" 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>                                                             \u884c 8 \u90a3\u7528\u6cd5\u89e3\u51b3\u4e4b\u540e<\/p>\n<p>\u5b9e\u9645\u4e0a\u903b\u8f91\u5c31\u662f\u68c0\u67e5 typename = decltype(std::declval&lt;F&gt;()(std::declval&lt;Args &amp;&amp;&gt;()&#8230;)) \u5408\u4e0d\u5408\u6cd5\uff0c\u8fdb\u4e00\u6b65\u6765\u8bf4\uff0c\u5c31\u53ea\u662f\u68c0\u67e5 declval&lt;\u4f20\u8fdb\u53bb\u7684\u90a3\u4e2a\u53c2\u6570&amp;&amp;&gt;() \u5408\u4e0d\u5408\u6cd5<\/p>\n<p>\u8fd9\u4e0d\u5c31\u8ddf stl \u7684\u5b9e\u73b0\u4e00\u6837\u7684                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2140528\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : hankai17 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u5934\u6655 \u597d\u590d\u6742                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2140529\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : wutiantong <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             &#8212; \u5982\u679c\u4e00\u4e2a lambda \u51fd\u6570\u4e0d\u6267\u884c\u662f\u4e0d\u662f\u5c31\u4e0d\u68c0\u67e5\u8fd4\u56de\u7c7b\u578b\u5408\u6cd5\u6027\u4e86\u5462?<\/p>\n<p>\u4f60\u7684\u8fd9\u4e2a\u95ee\u9898\u6ca1\u63d0\u5bf9\uff0c\u56e0\u4e3a\u4ee3\u7801\u4e2d\u76f8\u5173\u5904\u6240\u6d89\u53ca\u7684\u5e76\u975e\u666e\u901a\u7684 lambda \u800c\u662f generic lambda\uff0c\u5c31\u4f60\u7684\u8fd9\u4e2a\u95ee\u9898\u8fd9\u4e24\u8005\u662f\u5b8c\u5168\u4e0d\u540c\u7684\u3002                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>C++ \u6a21\u677f SFINAE \u95ee\u9898 &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/125388"}],"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=125388"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/125388\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=125388"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=125388"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=125388"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}