{"id":328,"date":"2020-03-21T11:33:23","date_gmt":"2020-03-21T03:33:23","guid":{"rendered":"https:\/\/www.notown.club\/?p=328"},"modified":"2020-08-22T11:03:34","modified_gmt":"2020-08-22T03:03:34","slug":"c%e5%8f%98%e9%87%8f%e5%92%8c%e5%9f%ba%e6%9c%ac%e7%b1%bb%e5%9e%8b%ef%bc%88%e7%ac%94%e8%ae%b0%e4%ba%8c%ef%bc%89","status":"publish","type":"post","link":"https:\/\/www.notown.top\/?p=328","title":{"rendered":"C++\u53d8\u91cf\u548c\u57fa\u672c\u7c7b\u578b\uff08\u7b14\u8bb0\u4e8c\uff09"},"content":{"rendered":"\n<p class=\"has-medium-font-size\"><strong>\u4e00\u3001\u5e38\u91cf\u8868\u8fbe\u5f0f\u548c constexpr <\/strong><\/p>\n\n\n\n<p>\u5e38\u91cf\u8868\u8fbe\u5f0f\u662f\u6307\u503c\u4e0d\u4f1a\u6539\u53d8\u5e76\u4e14\u5728\u7f16\u8bd1\u8fc7\u7a0b\u5c31\u80fd\u5f97\u5230\u8ba1\u7b97\u7ed3\u679c\u7684\u8868\u8fbe\u5f0f\uff0c\u5b57\u9762\u503c\u5c5e\u4e8e\u5e38\u91cf\u8868\u8fbe\u5f0f\u3002\u4e00\u4e2a\u5bf9\u8c61\uff08\u6216\u8868\u8fbe\u5f0f\uff09\u662f\u4e0d\u662f\u5e38\u91cf\u8868\u8fbe\u5f0f\u7531\u5b83\u7684\u6570\u636e\u7c7b\u578b\u548c\u521d\u59cb\u503c\u5171\u540c\u51b3\u5b9a\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>const int max_files =20 ;\/\/max_files\u662f\u5e38\u91cf\u8868\u8fbe\u5f0f<br>const int limit = max_files +1; \/\/limit\u662f\u5e38\u91cf\u8868\u8fbe\u5f0f<br>int staff_size =27;\/\/\u6570\u636e\u7c7b\u578b\u662f\u4e2a\u666e\u901a\u7684int\uff0cstaff_size\u5e76\u975e\u5e38\u91cf\u8868\u8fbe\u5f0f<br>const int sz = get_size();\/\/\u5c3d\u7ba1sz\u672c\u8eab\u662f\u4e00\u4e2a\u5e38\u91cf\uff0c\u4f46\u662f\u5177\u4f53\u503c\u8981\u5230\u8fd0\u884c\u65f6\u624d\u80fd\u5f97\u5230\uff0c\u6240\u4ee5sz\u4e0d\u662f\u5e38\u91cf\u8868\u8fbe\u5f0f<\/code><\/pre>\n\n\n\n<p>\u5229\u7528constexpr\u58f0\u660e\u53d8\u91cf\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u68c0\u9a8c\u53d8\u91cf\u662f\u5426\u4e3a\u4e00\u4e2a\u5e38\u91cf\u8868\u8fbe\u5f0f<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>constexpr int mf = 20;\/\/\u6b63\u786e<br>constexpr int limit = mf +1;\/\/\u6b63\u786e<br>constexpr sz = size();\/\/\u53ea\u6709\u5f53size\u662f\u4e00\u4e2aconstexpr\u51fd\u6570\u65f6\uff0c\u624d\u662f\u4e00\u6761\u6b63\u786e\u7684\u58f0\u660e<\/code><\/pre>\n\n\n\n<p>\uff08constexpr\u51fd\u6570\u8db3\u591f\u7b80\u5355\u4f7f\u5f97\u7f16\u8bd1\u65f6\u5c31\u53ef\u4ee5\u8ba1\u7b97\u5176\u7ed3\u679c\uff09<\/p>\n\n\n\n<p><strong>\u53ea\u6709\u5b57\u9762\u503c\u7c7b\u578b\uff08\u7b97\u672f\u7c7b\u578b\u3001\u5f15\u7528\u3001\u6307\u9488\uff09\u624d\u80fd\u5b9a\u4e49\u6210constexpr\uff0c\u81ea\u5b9a\u4e49\u7c7bSales_item\u3001IO\u5e93\u3001string\u7c7b\u578b\u4e0d\u5c5e\u4e8e\u5b57\u9762\u503c\u7c7b\u578b\uff0c\u5219\u4e0d\u80fd\u5b9a\u4e49\u6210constexpr\u3002<\/strong><br><strong>constexpr\u6307\u9488\u6307\u5411\u7684\u53d8\u91cf\u4e00\u822c\u53ea\u80fd\u5b9a\u4e49\u5728\u51fd\u6570\u4f53\u5916\uff0c\u5373\u53ea\u80fd\u6307\u5411\u5b58\u653e\u5728\u56fa\u5b9a\u5730\u5740\u4e2d\u7684\u53d8\u91cf\u3002<\/strong><\/p>\n\n\n\n<p class=\"has-medium-font-size\"><strong>\u4e8c\u3001\u7c7b\u578b\u522b\u540d<\/strong><\/p>\n\n\n\n<p>\u6709\u4e24\u79cd\u65b9\u5f0f\u5b9a\u4e49\u7c7b\u578b\u522b\u540d\uff1a<br><code>typedef unsigned int uint;<br>using sb = unsigned int;<\/code><br>\u5982\u679c\u67d0\u4e2a\u7c7b\u578b\u522b\u540d\u6307\u4ee3\u7684\u662f\u590d\u5408\u7c7b\u578b\u6216\u5e38\u91cf\uff0c\u90a3\u4e48\u628a\u5b83\u7528\u5728\u58f0\u660e\u8bed\u53e5\u91cc\uff0c\u5c31\u5f88\u5bb9\u6613\u51fa\u9519\u3002<br><code>typedef char* pstring;<br>const pstring cstr =0;\/\/cstr\u662f\u4e00\u4e2a\u5e38\u91cf\u6307\u9488\uff0c\u5b83\u6307\u5411char\u53d8\u91cf<br>const pstring <em>ps;\/\/<\/em>ps \u662f\u4e00\u4e2a\u5e38\u91cf\u6307\u9488\uff0c\u5b83\u6307\u5411char\u53d8\u91cf\u7684\u6307\u9488<\/code><br>\u5373\u5f53\u7c7b\u578b\u522b\u540d\u6307\u4ee3\u7684\u662f\u590d\u5408\u7c7b\u578b\u65f6\uff0c\u5c31\u4e0d\u662f\u7b80\u5355\u7684\u66ff\u6362\u7406\u89e3\u4e86\uff0cconst\u5f62\u5bb9\u7684\u662f\u6574\u4e2a\u522b\u540d\u3002<\/p>\n\n\n\n<p class=\"has-medium-font-size\"><strong>\u4e09\u3001auto\u3001decltype<\/strong><\/p>\n\n\n\n<p>auto\u6765\u5b9a\u4e49\u53d8\u91cf\uff0c\u7528\u5b83\u80fd\u8ba9\u7f16\u8bd1\u5668\u66ff\u6211\u4eec\u53bb\u5206\u6790\u8868\u8fbe\u5f0f\u6240\u5c5e\u7684\u7c7b\u578b\uff0c\u4f46\u5fc5\u987b\u8d4b\u521d\u503c\uff0c\u5f53\u7528auto\u540c\u65f6\u5b9a\u4e49\u4e24\u4e2a\u53d8\u91cf\u65f6\uff0c\u8fd9\u4e24\u4e2a\u53d8\u91cf\u7684\u7c7b\u578b\u9700\u8981\u4e00\u81f4\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/auto\u4e00\u822c\u4f1a\u5ffd\u7565\u6389\u9876\u5c42const\uff1a\n<code>int i =0;<\/code>\n<code>const int ci =i , &amp;cr=ci;\nauto b =ci ;\/\/b\u4e3a\u4e00\u4e2a\u6574\u6570(ci\u7684\u9876\u5c42const\u88ab\u5ffd\u7565\u4e86)\nauto c =cr ;\/\/c\u4e3a\u4e00\u4e2a\u6574\u6570\nauto d =&amp;i;\/\/d\u4e3a\u4e00\u4e2a\u6574\u5f62\u6307\u9488\nauto e =&amp;ci;\/\/e\u4e3a\u4e00\u4e2a\u6307\u5411\u6574\u5f62\u5e38\u91cf\u7684\u6307\u9488\uff08\u5bf9\u5e38\u91cf\u5bf9\u8c61\u53d6\u5730\u5740\u662f\u4e00\u79cd\u5e95\u5c42const\uff09<\/code>\n\/\/\u5982\u679c\u5e0c\u671b\u63a8\u65ad\u51fa\u6765\u7684auto\u7c7b\u578b\u662f\u4e00\u4e2a\u9876\u5c42const\uff0c\u9700\u8981\u660e\u786e\u6307\u51fa\n\/\/\u5982\uff1a\n <code>const auto f =ci;<\/code><\/pre>\n\n\n\n<p>decltype()  \u53ef\u7528\u6765\u6839\u636e\u9009\u62e9\u5e76\u8fd4\u56de\u64cd\u4f5c\u6570\u7684\u6570\u636e\u7c7b\u578b\uff0c\u76f8\u5f53\u4e8e\u4e00\u4e2a\u6570\u636e\u7c7b\u578b\uff08decltype\u4e0d\u4f1a\u5ffd\u7565\u9876\u5c42const\uff09<\/p>\n\n\n\n<p>\u5982\uff1a<br><code> const int ci =0, &amp;cj = ci;<br> decltype(ci) x =0\/\/x\u7684\u7c7b\u578b\u662fconst int <br> decltype(cj) y =x\/\/y\u7684\u7c7b\u578b\u662fconst int &amp;,\u5c06y\u7ed1\u5b9a\u5230\u53d8\u91cfx<\/code><\/p>\n\n\n\n<p><strong>\u5f15\u7528\u4ece\u6765\u90fd\u662f\u662f\u4f5c\u4e3a\u6240\u6307\u5bf9\u8c61\u7684\u540c\u4e49\u8bcd\u51fa\u73b0\uff0c\u4f46\u662f\u5728decltype\u8fd9\u91cc\u662f\u4e2a\u4f8b\u5916\uff0c\u5373decltype(var)\u65f6\uff0c\u53ea\u8981var\u662f\u5f15\u7528\uff0c\u5f97\u5230\u7684\u5c31\u662f\u5f15\u7528<\/strong><\/p>\n\n\n\n<p> decltype(*p) <br><code>int i =42, *<em>p = &amp;I,&amp;r=I; <\/em><br><em>decltype(r+0) b;\/\/b\u662f\u4e00\u4e2aint\u53d8\u91cf <\/em><br><em>decltype(<\/em>*p) c;\/\/\u9519\u8bef\uff0c\u8fd9\u6837\u7684\u8bdd decltype(*p)\u76f8\u5f53\u4e8eint &amp;\uff0c\u5b9a\u4e49\u5f15\u7528\u8981\u8d4b\u521d\u59cb\u503c<\/code>\u3002<\/p>\n\n\n\n<p>\u5982\u679cdecltype\u4f7f\u7528\u7684\u662f\u4e00\u4e2a\u4e0d\u52a0\u62ec\u53f7\u7684\u53d8\u91cf\uff0c\u5219\u5f97\u5230\u7684\u7ed3\u679c\u5c31\u662f\u8be5\u53d8\u91cf\u7c7b\u578b\uff1b\u5982\u679c\u7ed9\u53d8\u91cf\u52a0\u4e0a\u4e86\u4e00\u5c42\u6216\u591a\u5c42\u62ec\u53f7\uff0c\u5c31\u4f1a\u5f97\u5230\u4e00\u4e2a\u5f15\u7528\u7c7b\u578b<br> <code>decltype((i)) d;\u9519\u8bef\uff0cd\u662f\u4e00\u4e2aint&amp;\uff0c\u5fc5\u987b\u521d\u59cb\u5316\u3002<br> decltype(i) e;\u6b63\u786e\uff0ce\u662f\u4e00\u4e2aint<\/code><\/p>\n\n\n\n<p>\u8d4b\u503c\u4f1a\u4ea7\u751f\u5f15\u7528\u7684\u4e00\u7c7b\u578b\u8868\u8fbe\u5f0f\uff0c\u5f15\u7528\u7684\u7c7b\u578b\u5c31\u662f\u5de6\u503c\u7684\u7c7b\u578b<br> <code>int a = 3, b = 4;<br> decltype (a) c = a;<br> decltype (a = b) d = a;\/\/\u5219d\u662f\u4e00\u4e2aint&amp;\u7c7b\u578b\u3002<\/code><\/p>\n\n\n\n<p>\u4e2a\u4eba\u7406\u89e3\uff1a<\/p>\n\n\n\n<p>\u7531decltype\u6307\u5b9a\u7c7b\u578b\u548c\u7531auto\u6307\u5b9a\u7c7b\u578b\u7684\u533a\u522b<br>\n\u5229\u7528decltype\u548cauto\u90fd\u53ef\u4ee5\u5f88\u65b9\u4fbf\u7684\u5b9a\u4e49\u4e00\u4e2a\u53d8\u91cf\uff0cdecltype\u662f\u4ee5\u7c7b\u4f3c\u4e8e\u51fd\u6570\u7684\u5f62\u5f0f\u53d6\u67d0\u4e2a\u8868\u8fbe\u5f0f\u6216\u8005\u53d8\u91cf\u7684\u6570\u636e\u7c7b\u578b\uff0c\u5c06\u4ed6\u7528\u4e8e\u5b9a\u4e49\u65b0\u53d8\u91cf\uff0cauto\u662f\u6839\u636e\u8fd9\u4e2a\u65b0\u53d8\u91cf\u7684\u521d\u59cb\u5316\u65f6\u8d4b\u7684\u503c\u6216\u8005\u8868\u8fbe\u5f0f\u6765\u51b3\u5b9a\u8fd9\u4e2a\u65b0\u53d8\u91cf\u7684\u6570\u636e\u7c7b\u578b\u3002\u4f46decltype(var)\u91cc\uff0c\u53ea\u8981var\u662f\u4e00\u4e2a\u5f15\u7528\uff0c\u5219\u53ef\u4ee5\u76f4\u63a5\u5b9a\u4e49\u4e00\u4e2a\u5f15\u7528\uff0c\u800cauto\u5c31\u7b97\u8d4b\u4e00\u4e2a\u5f15\u7528\uff0c\u4e5f\u53ea\u4f1a\u8d4b\u4e00\u4e2a\u88ab\u5b83\u5f15\u7528\u7684\u5bf9\u8c61\u7684\u6570\u636e\u7c7b\u578b\uff0c\u8981\u662f\u771f\u60f3\u5b9a\u4e49\u4e00\u4e2a\u5f15\u7528\u9700\u8981\u81ea\u5df1\u624b\u52a8\u5728auto\u540e\u9762\u52a0&amp;\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001\u5e38\u91cf\u8868\u8fbe\u5f0f\u548c constexpr \u5e38\u91cf\u8868\u8fbe\u5f0f\u662f\u6307\u503c\u4e0d\u4f1a\u6539\u53d8\u5e76\u4e14\u5728\u7f16\u8bd1\u8fc7\u7a0b\u5c31\u80fd\u5f97\u5230\u8ba1\u7b97\u7ed3\u679c\u7684\u8868\u8fbe\u5f0f\uff0c\u5b57\u9762\u503c\u5c5e&hellip;<a href=\"https:\/\/www.notown.top\/?p=328\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">C++\u53d8\u91cf\u548c\u57fa\u672c\u7c7b\u578b\uff08\u7b14\u8bb0\u4e8c\uff09<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[11],"tags":[],"class_list":["post-328","post","type-post","status-publish","format-standard","hentry","category-c"],"_links":{"self":[{"href":"https:\/\/www.notown.top\/index.php?rest_route=\/wp\/v2\/posts\/328","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.notown.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.notown.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.notown.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.notown.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=328"}],"version-history":[{"count":5,"href":"https:\/\/www.notown.top\/index.php?rest_route=\/wp\/v2\/posts\/328\/revisions"}],"predecessor-version":[{"id":516,"href":"https:\/\/www.notown.top\/index.php?rest_route=\/wp\/v2\/posts\/328\/revisions\/516"}],"wp:attachment":[{"href":"https:\/\/www.notown.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=328"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.notown.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=328"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.notown.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=328"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}