{"id":209742,"date":"2020-11-29T10:56:07","date_gmt":"2020-11-29T02:56:07","guid":{"rendered":"http:\/\/4563.org\/?p=209742"},"modified":"2020-11-30T12:23:13","modified_gmt":"2020-11-30T04:23:13","slug":"c-%e6%a8%a1%e6%9d%bf%e7%bc%96%e7%a8%8b-variadic-template-unpack-%e7%96%91%e9%97%ae","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=209742","title":{"rendered":"C++ \u6a21\u677f\u7f16\u7a0b &#8211; variadic template &#8211; unpack \u7591\u95ee"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  C++ \u6a21\u677f\u7f16\u7a0b &#8211; variadic template &#8211; unpack \u7591\u95ee               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Stoulla <\/span>  <span><i><\/i> 0<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u6700\u8fd1\u7528 C++\u7684\u65f6\u5019\u9047\u5230\u4e86\u5bf9variadic template \u89e3\u5305\u7684\u95ee\u9898\uff0c\u5728 StackOverflow \u4e0a\u4e5f\u627e\u4e86\u597d\u591a\uff0c\u4f46\u59cb\u7ec8\u6ca1\u5f62\u6210\u4e00\u4e2a\u597d\u7684\u601d\u8def\uff0c\u6240\u4ee5\u6765 V \u7ad9\u6c42\u52a9\u4e0b\u3002<\/p>\n<p>\u95ee\u9898\u5982\u4e0b\uff1a<\/p>\n<p>\u91c7\u7528 C++ 14 \u3002<\/p>\n<p>\u51fd\u6570\u63a5\u53d7\u81f3\u5c11\u4e00\u4e2a\u53c2\u6570\uff0c\u53ef\u80fd\u6709\u591a\u4e2a\u53c2\u6570\uff0c\u7b2c\u4e00\u4e2a\u53c2\u6570 arg1 \u662f\u786e\u5b9a\u7684\u7c7b\u578b T \u3002\u540e\u9762\u7684\u53c2\u6570 args \u4f1a\u6709\u4e0d\u540c\u7684\u7c7b\u578b\uff0c \u5982 int\/ string \/custom_class \u7b49.<\/p>\n<p>\u6839\u636e args \u53c2\u6570\u4e0d\u540c\uff0c\u8c03\u7528\u4e0d\u540c\u7684\u51fd\u6570\uff0c\u6216\u8005\u662f\u8fd4\u56de bool \u7c7b\u578b\u3002<\/p>\n<ul>\n<li>\u6bd4\u5982\uff0c <code>args<\/code> \u4e2d\u542b int\uff0c \u5219\u8ba4\u4e3a\u4f20\u5165\u7684\u662f dim\uff0c \u6267\u884c <code>funcDim()<\/code>\uff1b<\/li>\n<li>\u82e5\u662f\uff0c <code>args<\/code> \u4e2d\u542b string\uff0c \u5219\u8ba4\u4e3a\u4f20\u5165\u7684\u662f name, \u6267\u884c <code>funName()<\/code>.<\/li>\n<li>\u4e0a\u8ff0\u7684 funcDim() , funcName() \u662f lambda \u51fd\u6570\uff0c\u4f46\u56e0\u4e3a\u89c9\u5f97\u5bf9\u95ee\u9898\u6ca1\u6709\u5e2e\u52a9\u6240\u4ee5\u7701\u7565\u3002<\/li>\n<\/ul>\n<p>\u60f3\u628a\u4e3b\u51fd\u6570\u6307\u9488\u5199\u6210\u5982\u4e0b\u5f62\u5f0f\uff1a<\/p>\n<pre><code>template &lt;typename T, typename ... Ts&gt; auto funcPtr = std::function&lt; bool (T arg1, Ts ... args)&gt;; <\/code><\/pre>\n<p>\u8bf7\u95ee\uff1a<\/p>\n<ol>\n<li>\u8fd9\u4e2a\u8981\u5982\u4f55\u5728\u8fd9\u79cd variadic functions \u4e2d\u8fdb\u884c\u89e3\u5305\u5462? \u7f51\u4e0a\u7684\u5927\u591a\u6570\u662f\u91c7\u7528 recursive \u7684\u65b9\u5f0f\uff0c\u4f46\u5982\u679c\u4e0d\u91c7\u7528 recursive \u65b9\u5f0f\uff0c\u5982\u4f55\u6bd4\u8f83\u7b80\u5355\u7684\u89e3\u5305? \u6211\u8ba4\u4e3a\u4e00\u4e2a\u6bd4\u8f83\u6e05\u695a\u7684\u65b9\u5f0f\u662f\u628a args \u8f6c\u6210 tuple\uff0c\u5373\u8c03\u7528<code>std::make_tuple(args ...)<\/code>\uff0c \u4f46\u8fd9\u4e2a\u611f\u89c9\u4e0d\u662f\u5f88\u4f18\u96c5\uff0c\u800c\u4e14\u8f6c\u6362\u4e00\u6b21\u6709\u6548\u7387\u635f\u5931\u3002\u8bf7\u95ee\u6709\u4ec0\u4e48\u597d\u7684\u65b9\u6cd5\u5462?<\/li>\n<li>\u5982\u679c\u8fd9\u79cd\u89e3\u5305\u4e0d\u53ef\u80fd\uff0c\u6211\u80fd\u5426\u5199\u4e09\u4e2a\u91cd\u8f7d\u51fd\u6570\uff0c\u7136\u540e\u7528\u540c\u4e00\u4e2a\u51fd\u6570\u6307\u9488\u4f5c\u4e3a\u5165\u53e3\uff0c \u5728\u8fd0\u884c\u65f6\u6307\u5411\u4e0d\u540c\u7684\u51fd\u6570? \u5373\u5199\u4e09\u4e2a\u51fd\u6570 func(arg1) , func(arg1, int1), func(arg1, string1) \u8fd9\u79cd\uff0c\u7136\u540e\u7528 funcPtr \u8fd0\u884c\u65f6\u52a8\u6001\u7684\u7ed1\u5b9a\u3002<\/li>\n<li>\u6709\u6ca1\u6709\u66f4\u4f18\u96c5\u7684\u65b9\u6848?<\/li>\n<\/ol><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>3<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"4358485\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : boyhailong <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u8fd9\u6837\u5b9a\u4e49\u672c\u8eab\u5c31\u5f88\u9ebb\u70e6\u4e86\u5427\uff0c\u8fd8\u4e0d\u5982 C \u91cc\u76f4\u63a5 void*\u7b80\u6d01\u5462 <img decoding=\"async\" src=\"http:\/\/4563.org\/wp-content\/uploads\/2020\/11\/504J5BO2-132.png\" alt=\"C++ \u6a21\u677f\u7f16\u7a0b - variadic template - unpack \u7591\u95ee\"> <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4358486\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Yienschwen <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4f60\u7684\u9700\u6c42\u4e0d\u662f\u7279\u522b\u6e05\u695a\u2026\u2026\u6240\u4ee5\u4e0d\u662f\u5f88\u660e\u767d\u4e3a\u4ec0\u4e48\u4f60\u89c9\u5f97\u9012\u5f52\u89e3\u5305\u4e0d\u65b9\u4fbf<\/p>\n<p>\u5173\u4e8e\u4f60\u95ee\u9898\u7684\u7b2c\u4e8c\u70b9\uff0c\u8fd9\u4f1a\u4ee4\u4f60\u7684\u5165\u53e3\u51fd\u6570\u80fd\u591f\u5728\u8fd0\u884c\u65f6\u63a5\u53d7\u4e0d\u540c\u7c7b\u578b\u7684\u53c2\u6570\uff0c\u6211\u611f\u89c9\u8fd9\u4e00\u70b9\u53ea\u6709\u7c7b\u4f3c\u4e8e[va_arg]( https:\/\/en.cppreference.com\/w\/cpp\/utility\/variadic\/va_arg)\uff0c\u4f46\u8fd9\u6837\u7c7b\u578b\u4fe1\u606f\u9700\u8981\u4f60\u9884\u5148\u77e5\u9053\uff0c\u53cd\u800c\u662f\u9759\u6001\u591a\u6001\u5bb9\u6613\u5904\u7406\u8fd9\u4e2a\u95ee\u9898\u3002                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4358487\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Yienschwen <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u5982\u679c\u4f60\u5bf9\u53c2\u6570\u7684\u987a\u5e8f\u6ca1\u6709\u4efb\u4f55\u7ea6\u675f\uff0c\u90a3\u6bd4\u5982\u4f60\u8981\u628a\u67d0\u4e2a`int`\u53d6\u51fa\u6765\u65f6\uff0c\u4f60\u5176\u5b9e\u4e5f\u4e0d\u77e5\u9053\u8fd9\u4e2a`int`\u5728\u4ec0\u4e48\u4f4d\u7f6e\uff0c\u90a3\u4f60\u7528`tuple`\u5176\u5b9e\u4e5f\u4e0d\u89e3\u51b3\u95ee\u9898\u3002\u53e6\u5916\uff0c\u5982\u679c\u4f60\u54ea\u6015\u7528\u4e86`tuple`\uff0c\u53ea\u8981\u4f60\u5c0f\u5fc3\u5730\u63a7\u5236\u662f\u5426\u62f7\u8d1d(\u6bd4\u5982\u7528`tuple&lt;const Targs&amp; &#8230;&gt;`\uff0c\u5176\u5b9e\u5927\u591a\u6570\u7f16\u8bd1\u5668\u90fd\u80fd\u4f18\u5316\uff0c\u6ca1\u6709\u4ec0\u4e48\u989d\u5916\u5f00\u9500\u3002<\/p>\n<p>\u6211\u5728 [godbolt]( https:\/\/godbolt.org\/z\/9j9zGv) \u5927\u6982\u770b\u4e86\u4e00\u4e0b\uff0c\u5e94\u8be5\u80fd\u8bf4\u660e\u95ee\u9898\u3002                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>C++ \u6a21\u677f\u7f16\u7a0b &#8211; &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\/209742"}],"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=209742"}],"version-history":[{"count":1,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/209742\/revisions"}],"predecessor-version":[{"id":209744,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/209742\/revisions\/209744"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=209742"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=209742"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=209742"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}