{"id":2037,"date":"2023-01-14T20:57:09","date_gmt":"2023-01-14T12:57:09","guid":{"rendered":"https:\/\/courtship.top\/?p=2037"},"modified":"2023-01-14T20:57:09","modified_gmt":"2023-01-14T12:57:09","slug":"%e5%8f%af%e5%8f%98%e5%8f%82%e6%95%b0%e6%a8%a1%e6%9d%bf-variadic-templates","status":"publish","type":"post","link":"https:\/\/courtship.top\/index.php\/2023\/01\/14\/%e5%8f%af%e5%8f%98%e5%8f%82%e6%95%b0%e6%a8%a1%e6%9d%bf-variadic-templates\/","title":{"rendered":"\u53ef\u53d8\u53c2\u6570\u6a21\u677f Variadic Templates"},"content":{"rendered":"\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>C++ Primer Plus<\/p>\n\n\n\n<p>CSDN : \u8d1d\u52d2\u91cc\u6069 http:\/\/t.csdn.cn\/4rtqi<\/p>\n<\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\">\u5b9a\u4e49<\/h2>\n\n\n\n<p>\u53ef\u53d8\u53c2\u6570\u6a21\u677f\u53ef\u4ee5\u521b\u5efa<strong>\u53ef\u53d8\u53c2\u6570\u7c7b<\/strong>\u548c<strong>\u53ef\u53d8\u53c2\u6570\u51fd\u6570<\/strong>\uff0c\u7528\u4e8e\u63a5\u6536\u4efb\u610f\u6570\u91cf\u7684\u53c2\u6570<\/p>\n\n\n\n<p>\u8981\u521b\u5efa\u53ef\u53d8\u53c2\u6570\u6a21\u677f\u9700\u8981\u7406\u89e3\u51e0\u4e2a\u8981\u70b9\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u6a21\u677f\u53c2\u6570\u5305<\/li>\n\n\n\n<li>\u51fd\u6570\u53c2\u6570\u5305<\/li>\n\n\n\n<li>\u5c55\u5f00\u53c2\u6570\u5305<\/li>\n\n\n\n<li>\u9012\u5f52<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\u6a21\u677f\u548c\u51fd\u6570\u53c2\u6570\u5305<\/h2>\n\n\n\n<p>C++11\u63d0\u4f9b\u4e86\u4e00\u4e2a\u7528\u7701\u7565\u53f7\u8868\u793a\u7684\u5143\u8fd0\u7b97\u7b26\uff0c\u7528\u4e8e\u8868\u793a\u6a21\u677f\u53c2\u6570\u5305\u7684\u6807\u8bc6\u7b26\u3002<\/p>\n\n\n\n<p>\u6a21\u677f\u53c2\u6570\u5305\u57fa\u672c\u4e0a\u662f\u4e00\u4e2a\u7c7b\u578b\u5217\u8868\uff08Args\uff09<\/p>\n\n\n\n<p>\u51fd\u6570\u53c2\u6570\u5305\u57fa\u672c\u4e0a\u662f\u4e00\u4e2a\u503c\u5217\u8868\uff08args\uff09<\/p>\n\n\n\n<p>Args\u548cT\u7684\u533a\u522b\u5728\u4e8e\uff0cT\u662f\u4e00\u79cd\u7c7b\u578b\u5339\u914d\uff0c\u800cArgs\u4e0e\u4efb\u610f\u6570\u91cf\u5305\u62ec0\u7684\u7c7b\u578b\u5339\u914d<\/p>\n\n\n\n<p><strong>\u51fd\u6570\u53c2\u6570\u5305args\u5305\u542b\u7684\u503c\u5217\u8868\u4e0e\u6a21\u677f\u53c2\u6570\u5305Args\u5305\u542b\u7684\u7c7b\u578b\u5217\u8868\u5339\u914d\uff0c\u65e0\u8bba\u662f\u7c7b\u578b\u8fd8\u662f\u6570\u91cf<\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u5c55\u5f00\u53c2\u6570\u5305<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">ANSI C\u539f\u578b\u7684\u53ef\u53d8\u53c2\u6570\u5217\u8868<\/h3>\n\n\n\n<p>ANSI C\u501f\u9274\u4e86C++\u4e2d\u7684\u539f\u578b\uff0c\u4f46\u8fd9\u4e24\u79cd\u8bed\u8a00\u8fd8\u6709\u6709\u533a\u522b\u7684\u3002\u5176\u4e2d\u6700\u91cd\u8981\u7684\u533a\u522b\u662f\uff0c\u4e3a\u4e0e\u57fa\u672cC\u517c\u5bb9\uff0cANSI C\u4e2d\u7684\u539f\u578b\u662f\u53ef\u9009\u7684\uff0c\u4f46\u5728C++\u4e2d\uff0c\u539f\u578b\u662f\u5fc5\u4e0d\u53ef\u5c11\u7684\u3002\u4f8b\u5982\uff0c\u8bf7\u770b\u4e0b\u9762\u7684\u51fd\u6570\u58f0\u660e\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void say_hi();<\/code><\/pre>\n\n\n\n<p>\u5728C++\u4e2d\uff0c\u62ec\u53f7\u4e3a\u7a7a\u4e0e\u5728\u62ec\u53f7\u4e2d\u4f7f\u7528\u5173\u952e\u5b57void\u662f\u7b49\u6548\u7684\u2013\u610f\u5473\u7740\u51fd\u6570\u6ca1\u6709\u53c2\u6570\u3002\u5728ANSI C\u4e2d\uff0c\u62ec\u53f7\u4e3a\u7a7a\u610f\u5473\u7740\u4e0d\u6307\u5b9a\u53c2\u6570\u2013\u8fd9\u610f\u5473\u7740\u5728\u540e\u9762\u5b9a\u4e49\u53c2\u6570\u5217\u8868\u3002\u5728C++\u4e2d\uff0c\u4e0d\u6307\u5b9a\u53c2\u6570\u5217\u8868\u65f6\u5e94\u4f7f\u7528\u7701\u7565\u53f7\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void say_hi(\u2026);<\/code><\/pre>\n\n\n\n<p>\u901a\u5e38\uff0c\u4ec5\u5f53\u4e0e\u63a5\u53d7\u53ef\u53d8\u53c2\u6570\u7684C\u51fd\u6570\uff08\u5982printf()\uff09\u4ea4\u4e92\u65f6\u624d\u9700\u8981\u8fd9\u6837\u505a\u3002<\/p>\n\n\n\n<p>\u6211\u4eec\u975e\u5e38\u719f\u6089\u7684printf\u51fd\u6570\u5c31\u662f\u4e00\u79cd\u53d8\u957f\u53c2\u6570\uff0c\u7b80\u5316\u7248\u672c\u7684\u51fd\u6570\u539f\u578b\u5373\u4e3a\uff1aint printf(const char * _Format,\u2026);\uff0c\u5176\u4e2d\uff0c\u7b2c\u4e00\u4e2a\u53c2\u6570\u5373\u4e3a\u5fc5\u987b\u5236\u5b9a\u7684\u683c\u5f0f\u5b57\u7b26\u4e32\uff0c\u540e\u9762\u7684\u7701\u7565\u53f7\u8868\u793a\u6570\u91cf\u4e0d\u5b9a\u7684\u53c2\u6570\u5217\u8868\u3002<\/p>\n\n\n\n<p>\u4f7f\u7528\u4e0a\u9762\u63d0\u5230\u7684\u4e09\u4e2a\u70b9\u8868\u793a\u7684\u7701\u7565\u53f7\u5373\u53ef\u8fbe\u5230\u5b9a\u4e49\u4e00\u4e2a\u53d8\u957f\u53c2\u6570\u7684\u51fd\u6570\u7684\u76ee\u7684\uff0c\u4f46\u662f\u51fd\u6570\u4e2d\u5982\u679c\u83b7\u53d6\u8fd9\u91cc\u9762\u7684\u6240\u6709\u53c2\u6570\u5462\uff1f<\/p>\n\n\n\n<p>\u8fd9\u662f\u8981\u4f7f\u7528C\u8bed\u8a00\u4e2d\u89e3\u51b3\u53d8\u957f\u53c2\u6570\u95ee\u9898\u7684\u82e5\u5e72\u5b8f\u5b9a\u4e49<strong>va_start\u3001va_arg\u3001va_end<\/strong>\uff0c\u5b83\u4eec\u5747\u5b9a\u4e49\u5728stdarg.h\u5934\u6587\u4ef6\u4e2d\uff0c\u4ee5va\u5f00\u5934\uff08\u8868\u793avariable-argument\u53ef\u53d8\u53c2\u6570\uff09\uff0c\u53ef\u6839\u636e\u9884\u5148\u5b9a\u4e49\u7684\u7cfb\u7edf\u5e73\u53f0\u81ea\u52a8\u83b7\u53d6\u76f8\u5e94\u5e73\u53f0\u4e0a\u5404\u4e2a\u6570\u636e\u7c7b\u578b\u7684\u504f\u79fb\u91cf\u3002\u5b83\u4eec\u7684\u4f7f\u7528\u65b9\u6cd5\u4e3a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>va_list ap; \/\/\u5b9a\u4e49\u4e00\u4e2a\u53ef\u53d8\u53c2\u6570\u5217\u8868ap\nva_start(ap,arg); \/\/\u521d\u59cb\u5316ap\u6307\u5411\u53c2\u6570arg\u7684\u4e0b\u4e00\u4e2a\u53c2\u6570\nva_arg(ap,type); \/\/\u83b7\u53d6\u5f53\u524d\u53c2\u6570\u5185\u5bb9\u5e76\u5c06ap\u6307\u5411\u4e0b\u4e00\u4e2a\u53c2\u6570\nva_end(ap); \/\/\u91ca\u653eap <\/code><\/pre>\n\n\n\n<p>\u5b9e\u4f8b\u5982\u4e0b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int find_sum(int num, ...)\n{\n\tva_list ap;\n\tva_start(ap, num);\n\tint sum = 0;\n\tfor (int i = 0; i &lt; num; i++)\n\t\tsum += va_arg(ap, int);\n\tva_end(ap);\n\n\treturn sum;\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">C++\u89c4\u5219<\/h2>\n\n\n\n<h4 class=\"wp-block-heading\">\u4e3a\u4ec0\u4e48C++\u7684\u505a\u6cd5\u4e0d\u540c\uff1f<\/h4>\n\n\n\n<p>\u6309ANSI C\u7684\u505a\u6cd5\u662f\u6709\u4e00\u5b9a\u95ee\u9898\u7684\uff0c\u6bd4\u5982\u4f1a\u9020\u6210\u65e0\u9650\u9012\u5f52\uff1a<\/p>\n\n\n\n<p>\u5b9a\u4e49\u51fd\u6570<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void func(1,\"haha\",9.6)<\/code><\/pre>\n\n\n\n<p>\u628a1,\"haha\",9.6\u5c01\u88c5\u5230args\u4e2d\u5f97<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void func(args...)<\/code><\/pre>\n\n\n\n<p>\u5728\u51fd\u6570\u5185\u90e8\u8c03\u7528func(args...),\u5c06\u4f1a\u5c55\u5f00\u6210func(1,\"haha\",9.6),\u4e5f\u5c31\u662f\u8bf4\u5b83\u4f1a\u4e0d\u505c\u7684\u8c03\u7528\u81ea\u5df1\uff0c<strong>\u9020\u6210\u65e0\u9650\u9012\u5f52<\/strong><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u5728\u53ef\u53d8\u53c2\u6570\u6a21\u677f\u4e2d\u4f7f\u7528\u9012\u5f52<\/h3>\n\n\n\n<p>\u6838\u5fc3\u7406\u5ff5\u4e3a\u5c06\u51fd\u6570\u53c2\u6570\u5305\u5c55\u5f00\uff0c\u5bf9\u5217\u8868\u4e2d\u7684\u7b2c\u4e00\u9879\u8fdb\u884c\u5904\u7406\uff0c\u518d\u5c06\u4f59\u4e0b\u7684\u5185\u5bb9\u4f20\u9012\u7ed9\u9012\u5f52\u8c03\u7528\uff0c\u4ee5\u6b64\u7c7b\u63a8\u76f4\u5230\u5217\u8868\u4e3a\u7a7a\u3002\u786e\u4fdd\u9012\u5f52\u7ed3\u675f\u6781\u5176\u91cd\u8981<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>template &lt;typename T,typename... Args&gt;\nvoid func(T value,Args...args){\n}<\/code><\/pre>\n\n\n\n<p><strong>\u5bf9\u4e8e\u4e0a\u8ff0\u5b9a\u4e49\uff0c\u51fd\u6570\u7684\u7b2c\u4e00\u4e2a\u5b9e\u53c2\u51b3\u5b9a\u4e86T\u548cvalue\u7684\u503c\uff0c\u800c\u5176\u4ed6\u5b9e\u53c2\u51b3\u5b9a\u4e86Args\u548cargs\u7684\u503c\u3002\u7136\u540e\u9012\u5f52\u8c03\u7528func\uff0c\u5e76\u4ee5args...\u7684\u65b9\u5f0f\u5c06\u5176\u4ed6\u5b9e\u53c2\u4f20\u9012\u7ed9\u4ed6\u3002\u6bcf\u6b21\u4f20\u9012\u90fd\u4f1a\u663e\u793a\u4e00\u4e2a\u503c\uff0c\u5e76\u4f20\u9012\u7f29\u77ed\u4e86\u7684\u51fd\u6570\u53c2\u6570\u5217\u8868\uff0c\u76f4\u5230\u5217\u8868\u4e3a\u7a7a\u4e3a\u6b62\u3002<\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Args\u548cargs\u7684size<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>template &lt;typename... Args&gt;\nvoid g(Args... args)\n{\n    std::cout &lt;&lt; sizeof...(Args) &lt;&lt; std::endl;\n\n    std::cout &lt;&lt; sizeof...(args) &lt;&lt; std::endl;\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>C++ Primer Plus CSDN : \u8d1d\u52d2\u91cc\u6069 http:\/\/t.csdn.cn\/4rtqi \u5b9a\u4e49 \u53ef\u53d8\u53c2\u6570\u6a21\u677f\u53ef\u4ee5\u521b\u5efa\u53ef &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"emotion":"","emotion_color":"","title_style":"","license":"","footnotes":""},"categories":[22],"tags":[],"class_list":["post-2037","post","type-post","status-publish","format-standard","hentry","category-c"],"_links":{"self":[{"href":"https:\/\/courtship.top\/index.php\/wp-json\/wp\/v2\/posts\/2037","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/courtship.top\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/courtship.top\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/courtship.top\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/courtship.top\/index.php\/wp-json\/wp\/v2\/comments?post=2037"}],"version-history":[{"count":0,"href":"https:\/\/courtship.top\/index.php\/wp-json\/wp\/v2\/posts\/2037\/revisions"}],"wp:attachment":[{"href":"https:\/\/courtship.top\/index.php\/wp-json\/wp\/v2\/media?parent=2037"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/courtship.top\/index.php\/wp-json\/wp\/v2\/categories?post=2037"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/courtship.top\/index.php\/wp-json\/wp\/v2\/tags?post=2037"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}