{"id":1995,"date":"2023-01-03T17:15:36","date_gmt":"2023-01-03T09:15:36","guid":{"rendered":"https:\/\/courtship.top\/?p=1995"},"modified":"2023-01-03T17:15:36","modified_gmt":"2023-01-03T09:15:36","slug":"constexpr","status":"publish","type":"post","link":"https:\/\/courtship.top\/index.php\/2023\/01\/03\/constexpr\/","title":{"rendered":"constexpr"},"content":{"rendered":"\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u8f6c\u81eaC\u8bed\u8a00\u4e2d\u6587\u7f51<\/p>\n<cite>http:\/\/c.biancheng.net\/view\/7781.html<\/cite><\/blockquote>\n\n\n\n<p>constexpr \u662f C++ 11 \u6807\u51c6\u65b0\u5f15\u5165\u7684\u5173\u952e\u5b57\uff0c\u4e0d\u8fc7\u5728\u8bb2\u89e3\u5176\u5177\u4f53\u7528\u6cd5\u548c\u529f\u80fd\u4e4b\u524d\uff0c\u8bfb\u8005\u9700\u8981\u5148\u641e\u6e05\u695a C++ \u5e38\u91cf\u8868\u8fbe\u5f0f\u7684\u542b\u4e49\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u5e38\u91cf\u8868\u8fbe\u5f0f<\/h2>\n\n\n\n<p>\u6240\u8c13\u5e38\u91cf\u8868\u8fbe\u5f0f\uff0c\u6307\u7684\u5c31\u662f\u7531\u591a\u4e2a\uff08\u22651\uff09\u5e38\u91cf\u7ec4\u6210\u7684\u8868\u8fbe\u5f0f\u3002<strong>\u6362\u53e5\u8bdd\u8bf4\uff0c\u5982\u679c\u8868\u8fbe\u5f0f\u4e2d\u7684\u6210\u5458\u90fd\u662f\u5e38\u91cf\uff0c\u90a3\u4e48\u8be5\u8868\u8fbe\u5f0f\u5c31\u662f\u4e00\u4e2a\u5e38\u91cf\u8868\u8fbe\u5f0f\u3002\u8fd9\u4e5f\u610f\u5473\u7740\uff0c\u5e38\u91cf\u8868\u8fbe\u5f0f\u4e00\u65e6\u786e\u5b9a\uff0c\u5176\u503c\u5c06\u65e0\u6cd5\u4fee\u6539\u3002<\/strong><\/p>\n\n\n\n<p>\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u6211\u4eec\u7ecf\u5e38\u4f1a\u7528\u5230\u5e38\u91cf\u8868\u8fbe\u5f0f\u3002\u4ee5\u5b9a\u4e49\u6570\u7ec4\u4e3a\u4f8b\uff0c\u6570\u7ec4\u7684\u957f\u5ea6\u5c31\u5fc5\u987b\u662f\u4e00\u4e2a\u5e38\u91cf\u8868\u8fbe\u5f0f\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ 1)\nint url&#91;10];\/\/\u6b63\u786e\n\/\/ 2)\nint url&#91;6 + 4];\/\/\u6b63\u786e\n\/\/ 3)\nint length = 6;\nint url&#91;length];\/\/\u9519\u8bef\uff0clength\u662f\u53d8\u91cf<\/code><\/pre>\n\n\n\n<p>\u4e0a\u8ff0\u4ee3\u7801\u6f14\u793a\u4e86 3 \u79cd\u5b9a\u4e49 url \u6570\u7ec4\u7684\u65b9\u5f0f\uff0c\u5176\u4e2d\u7b2c 1\u30012 \u79cd\u5b9a\u4e49 url \u6570\u7ec4\u65f6\uff0c\u957f\u5ea6\u5206\u522b\u4e3a 10 \u548c 6+4\uff0c\u663e\u7136\u5b83\u4eec\u90fd\u662f\u5e38\u91cf\u8868\u8fbe\u5f0f\uff0c\u53ef\u4ee5\u7528\u4e8e\u8868\u793a\u6570\u7ec4\u7684\u957f\u5ea6\uff1b<strong>\u7b2c 3 \u79cd url \u6570\u7ec4\u7684\u957f\u5ea6\u4e3a length\uff0c\u5b83\u662f\u53d8\u91cf\u800c\u975e\u5e38\u91cf\uff0c\u56e0\u6b64\u4e0d\u662f\u4e00\u4e2a\u5e38\u91cf\u8868\u8fbe\u5f0f\uff0c\u65e0\u6cd5\u7528\u4e8e\u8868\u793a\u6570\u7ec4\u7684\u957f\u5ea6\u3002<\/strong><\/p>\n\n\n\n<p>\u6211\u4eec\u77e5\u9053\uff0cC++ \u7a0b\u5e8f\u7684\u6267\u884c\u8fc7\u7a0b\u5927\u81f4\u8981\u7ecf\u5386<strong>\u7f16\u8bd1\u3001\u94fe\u63a5\u3001\u8fd0\u884c<\/strong>\u8fd9 3 \u4e2a\u9636\u6bb5\u3002\u503c\u5f97\u4e00\u63d0\u7684\u662f\uff0c<strong>\u5e38\u91cf\u8868\u8fbe\u5f0f\u548c\u975e\u5e38\u91cf\u8868\u8fbe\u5f0f\u7684\u8ba1\u7b97\u65f6\u673a\u4e0d\u540c\uff0c\u975e\u5e38\u91cf\u8868\u8fbe\u5f0f\u53ea\u80fd\u5728\u7a0b\u5e8f\u8fd0\u884c\u9636\u6bb5\u8ba1\u7b97\u51fa\u7ed3\u679c\uff1b\u800c\u5e38\u91cf\u8868\u8fbe\u5f0f\u7684\u8ba1\u7b97\u5f80\u5f80\u53d1\u751f\u5728\u7a0b\u5e8f\u7684\u7f16\u8bd1\u9636\u6bb5\uff0c\u8fd9\u53ef\u4ee5\u6781\u5927\u63d0\u9ad8\u7a0b\u5e8f\u7684\u6267\u884c\u6548\u7387\uff0c\u56e0\u4e3a\u8868\u8fbe\u5f0f\u53ea\u9700\u8981\u5728\u7f16\u8bd1\u9636\u6bb5\u8ba1\u7b97\u4e00\u6b21\uff0c\u8282\u7701\u4e86\u6bcf\u6b21\u7a0b\u5e8f\u8fd0\u884c\u65f6\u90fd\u9700\u8981\u8ba1\u7b97\u4e00\u6b21\u7684\u65f6\u95f4\u3002<\/strong><\/p>\n\n\n\n<p>\u5bf9\u4e8e\u7528 C++ \u7f16\u5199\u7684\u7a0b\u5e8f\uff0c\u6027\u80fd\u5f80\u5f80\u662f\u6c38\u6052\u7684\u8ffd\u6c42\u3002<strong>\u90a3\u4e48\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u5982\u4f55\u624d\u80fd\u5224\u5b9a\u4e00\u4e2a\u8868\u8fbe\u5f0f\u662f\u5426\u4e3a\u5e38\u91cf\u8868\u8fbe\u5f0f\uff0c\u8fdb\u800c\u83b7\u5f97\u5728\u7f16\u8bd1\u9636\u6bb5\u5373\u53ef\u6267\u884c\u7684\u201c\u7279\u6743\u201d\u5462\uff1f\u9664\u4e86\u4eba\u4e3a\u5224\u5b9a\u5916\uff0cC++11 \u6807\u51c6\u8fd8\u63d0\u4f9b\u6709 constexpr \u5173\u952e\u5b57\u3002<\/strong><\/p>\n\n\n\n<p><strong>constexpr \u5173\u952e\u5b57\u7684\u529f\u80fd\u662f\u4f7f\u6307\u5b9a\u7684\u5e38\u91cf\u8868\u8fbe\u5f0f\u83b7\u5f97\u5728\u7a0b\u5e8f\u7f16\u8bd1\u9636\u6bb5\u8ba1\u7b97\u51fa\u7ed3\u679c\u7684\u80fd\u529b\uff0c\u800c\u4e0d\u5fc5\u7b49\u5230\u7a0b\u5e8f\u8fd0\u884c\u9636\u6bb5\u3002C++ 11 \u6807\u51c6\u4e2d\uff0cconstexpr \u53ef\u7528\u4e8e\u4fee\u9970\u666e\u901a\u53d8\u91cf\u3001\u51fd\u6570\uff08\u5305\u62ec\u6a21\u677f\u51fd\u6570\uff09\u4ee5\u53ca\u7c7b\u7684\u6784\u9020\u51fd\u6570\u3002<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-pullquote\"><blockquote><p>C++ 11\u6807\u51c6\u4e2d\uff0c\u4e3a\u4e86\u89e3\u51b3 const \u5173\u952e\u5b57\u7684\u53cc\u91cd\u8bed\u4e49\u95ee\u9898\uff0c\u4fdd\u7559\u4e86 const \u8868\u793a\u201c\u53ea\u8bfb\u201d\u7684\u8bed\u4e49\uff0c\u800c\u5c06\u201c\u5e38\u91cf\u201d\u7684\u8bed\u4e49\u5212\u5206\u7ed9\u4e86\u65b0\u6dfb\u52a0\u7684 constexpr \u5173\u952e\u5b57\u3002\u56e0\u6b64 C++11 \u6807\u51c6\u4e2d\uff0c\u5efa\u8bae\u5c06 const \u548c constexpr \u7684\u529f\u80fd\u533a\u5206\u5f00\uff0c\u5373\u51e1\u662f\u8868\u8fbe\u201c\u53ea\u8bfb\u201d\u8bed\u4e49\u7684\u573a\u666f\u90fd\u4f7f\u7528 const\uff0c\u8868\u8fbe\u201c\u5e38\u91cf\u201d\u8bed\u4e49\u7684\u573a\u666f\u90fd\u4f7f\u7528 constexpr\u3002<\/p><\/blockquote><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">constexpr\u4fee\u9970\u53d8\u91cf<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u4fee\u9970\u666e\u901a\u53d8\u91cf<\/h3>\n\n\n\n<p>C++11 \u6807\u51c6\u4e2d\uff0c\u5b9a\u4e49\u53d8\u91cf\u65f6\u53ef\u4ee5\u7528 constexpr \u4fee\u9970\uff0c\u4ece\u800c\u4f7f\u8be5\u53d8\u91cf\u83b7\u5f97\u5728\u7f16\u8bd1\u9636\u6bb5\u5373\u53ef\u8ba1\u7b97\u51fa\u7ed3\u679c\u7684\u80fd\u529b\u3002<\/p>\n\n\n\n<p>\u503c\u5f97\u4e00\u63d0\u7684\u662f\uff0c\u4f7f\u7528 constexpr \u4fee\u6539\u666e\u901a\u53d8\u91cf\u65f6\uff0c\u53d8\u91cf\u5fc5\u987b\u7ecf\u8fc7\u521d\u59cb\u5316\u4e14\u521d\u59cb\u503c\u5fc5\u987b\u662f\u4e00\u4e2a\u5e38\u91cf\u8868\u8fbe\u5f0f\u3002\u4e3e\u4e2a\u4f8b\u5b50\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int main()\n{\n    constexpr int num = 1 + 2 + 3;\n    int url&#91;num] = {1,2,3,4,5,6};\n    couts&lt;&lt; url&#91;1] &lt;&lt; endl;\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>\u53ef\u4ee5\u770b\u5230\uff0c\u7a0b\u5e8f\u7b2c 6 \u884c\u4f7f\u7528 constexpr \u4fee\u9970 num \u53d8\u91cf\uff0c\u540c\u65f6\u5c06 \"1+2+3\" \u8fd9\u4e2a\u5e38\u91cf\u8868\u8fbe\u5f0f\u8d4b\u503c\u7ed9 num\u3002\u7531\u6b64\uff0c\u7f16\u8bd1\u5668\u5c31\u53ef\u4ee5\u5728\u7f16\u8bd1\u65f6\u671f\u5bf9 num \u8fd9\u4e2a\u8868\u8fbe\u5f0f\u8fdb\u884c\u8ba1\u7b97\uff0c\u56e0\u4e3a num \u53ef\u4ee5\u4f5c\u4e3a\u5b9a\u4e49\u6570\u7ec4\u65f6\u7684\u957f\u5ea6\u3002<\/p>\n\n\n\n<p>\u5f53\u5e38\u91cf\u8868\u8fbe\u5f0f\u4e2d\u5305\u542b\u6d6e\u70b9\u6570\u65f6\uff0c\u8003\u8651\u5230\u7a0b\u5e8f\u7f16\u8bd1\u548c\u8fd0\u884c\u6240\u5728\u7684\u7cfb\u7edf\u73af\u5883\u53ef\u80fd\u4e0d\u540c\uff0c\u5e38\u91cf\u8868\u8fbe\u5f0f\u5728\u7f16\u8bd1\u9636\u6bb5\u548c\u8fd0\u884c\u9636\u6bb5\u8ba1\u7b97\u51fa\u7684\u7ed3\u679c\u7cbe\u5ea6\u5f88\u53ef\u80fd\u4f1a\u53d7\u5230\u5f71\u54cd\uff0c\u56e0\u6b64 C++11 \u6807\u51c6\u89c4\u5b9a\uff0c\u6d6e\u70b9\u5e38\u91cf\u8868\u8fbe\u5f0f\u5728\u7f16\u8bd1\u9636\u6bb5\u8ba1\u7b97\u7684\u7cbe\u5ea6\u8981\u81f3\u5c11\u7b49\u4e8e\uff08\u6216\u8005\u9ad8\u4e8e\uff09\u8fd0\u884c\u9636\u6bb5\u8ba1\u7b97\u51fa\u7684\u7cbe\u5ea6\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u4fee\u9970\u51fd\u6570<\/h3>\n\n\n\n<p><strong>constexpr \u8fd8\u53ef\u4ee5\u7528\u4e8e\u4fee\u9970\u51fd\u6570\u7684\u8fd4\u56de\u503c\uff0c\u8fd9\u6837\u7684\u51fd\u6570\u53c8\u79f0\u4e3a\u201c\u5e38\u91cf\u8868\u8fbe\u5f0f\u51fd\u6570\u201d\u3002<\/strong><\/p>\n\n\n\n<p>\u6ce8\u610f\uff0cconstexpr \u5e76\u975e\u53ef\u4ee5\u4fee\u6539\u4efb\u610f\u51fd\u6570\u7684\u8fd4\u56de\u503c\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u4e00\u4e2a\u51fd\u6570\u8981\u60f3\u6210\u4e3a\u5e38\u91cf\u8868\u8fbe\u5f0f\u51fd\u6570\uff0c\u5fc5\u987b\u6ee1\u8db3\u5982\u4e0b 4 \u4e2a\u6761\u4ef6\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u5e38\u91cf\u8868\u8fbe\u5f0f\u51fd\u6570\u5fc5\u987b\u6709\u8fd4\u56de\u503c\uff08\u4e0d\u53ef\u4ee5\u662fvoid\u51fd\u6570\uff09<\/li>\n\n\n\n<li>\u5e38\u91cf\u8868\u8fbe\u5f0f\u51fd\u6570\u4f53\u4e2d\u53ea\u80fd\u6709\u4e00\u6761\u8bed\u53e5\uff0c\u4e14\u8be5\u8bed\u53e5\u5fc5\u987b\u662freturn\u8bed\u53e5\u3002\uff08\u53ef\u4ee5\u4f7f\u7528?:\u3001\u9012\u5f52\uff09\u4f46\u4e0d\u4ea7\u751f\u5b9e\u9645\u4ee3\u7801\u7684\u8bed\u53e5\u53ef\u4ee5\u5728\u5e38\u91cf\u8868\u8fbe\u5f0f\u51fd\u6570\u4e2d\u4f7f\u7528\uff0c\u5982static_assert,using,typedef\u7b49\uff08\u8fd9\u6761\u89c4\u5b9a\u5728C++14\u4e2d\u5927\u5e45\u653e\u677e\uff09<\/li>\n\n\n\n<li>return\u8bed\u53e5\u4e2d\uff0c\u4e0d\u80fd\u4f7f\u7528\u975e\u5e38\u91cf\u8868\u8fbe\u5f0f\u7684\u53d8\u91cf\u3001\u51fd\u6570\uff0c\u4e14return\u7684\u8868\u8fbe\u5f0f\u4e5f\u8981\u662f\u5e38\u91cf\u8868\u8fbe\u5f0f<\/li>\n\n\n\n<li>\u5e38\u91cf\u8868\u8fbe\u5f0f\u51fd\u6570\u5728\u4f7f\u7528\u524d\uff0c\u5fc5\u987b\u6709\u5b9a\u4e49\u3002\uff08\u666e\u901a\u51fd\u6570\u5728\u88ab\u8c03\u7528\u524d\u53ea\u8981\u6709\u51fd\u6570\u58f0\u660e\u5c31\u591f\u4e86\uff0c\u4e0d\u4e00\u5b9a\u6709\u5b9a\u4e49\uff09<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">\u4fee\u9970\u6784\u9020\u51fd\u6570<\/h3>\n\n\n\n<p>\u5bf9\u4e8e C++ \u5185\u7f6e\u7c7b\u578b\u7684\u6570\u636e\uff0c\u53ef\u4ee5\u76f4\u63a5\u7528 constexpr \u4fee\u9970\uff0c\u4f46\u5982\u679c\u662f\u81ea\u5b9a\u4e49\u7684\u6570\u636e\u7c7b\u578b\uff08\u7528 struct \u6216\u8005 class \u5b9e\u73b0\uff09\uff0c\u76f4\u63a5\u7528 constexpr \u4fee\u9970\u662f\u4e0d\u884c\u7684\u3002<\/p>\n\n\n\n<p>\u5f53\u6211\u4eec\u60f3\u81ea\u5b9a\u4e49\u4e00\u4e2a\u53ef\u4ea7\u751f\u5e38\u91cf\u7684\u7c7b\u578b\u65f6\uff0c\u6b63\u786e\u7684\u505a\u6cd5\u662f\u5728\u8be5\u7c7b\u578b\u7684\u5185\u90e8\u6dfb\u52a0\u4e00\u4e2a\u5e38\u91cf\u6784\u9020\u51fd\u6570\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/\u81ea\u5b9a\u4e49\u7c7b\u578b\u7684\u5b9a\u4e49\nstruct myType {\n    constexpr myType(char *name,int age):name(name),age(age){};\n    const char* name;\n    int age;\n    \/\/\u5176\u5b83\u7ed3\u6784\u4f53\u6210\u5458\n};\n\nint main()\n{\n    constexpr struct myType mt { \"zhangsan\", 10 };\n    cout &lt;&lt; mt.name &lt;&lt; \" \" &lt;&lt; mt.age &lt;&lt; endl;\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>\u6ce8\u610f\uff0cconstexpr \u4fee\u9970\u7c7b\u7684\u6784\u9020\u51fd\u6570\u65f6\uff0c\u8981\u6c42\u8be5\u6784\u9020\u51fd\u6570\u7684\u51fd\u6570\u4f53\u5fc5\u987b\u4e3a\u7a7a\uff0c\u4e14\u91c7\u7528\u521d\u59cb\u5316\u5217\u8868\u7684\u65b9\u5f0f\u4e3a\u5404\u4e2a\u6210\u5458\u8d4b\u503c\u65f6\uff0c\u5fc5\u987b\u4f7f\u7528\u5e38\u91cf\u8868\u8fbe\u5f0f\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u4fee\u9970\u6a21\u677f\u51fd\u6570<\/h3>\n\n\n\n<p>C++11 \u8bed\u6cd5\u4e2d\uff0cconstexpr \u53ef\u4ee5\u4fee\u9970\u6a21\u677f\u51fd\u6570\uff0c\u4f46\u7531\u4e8e\u6a21\u677f\u4e2d\u7c7b\u578b\u7684\u4e0d\u786e\u5b9a\u6027\uff0c\u56e0\u6b64\u6a21\u677f\u51fd\u6570\u5b9e\u4f8b\u5316\u540e\u7684\u51fd\u6570\u662f\u5426\u7b26\u5408\u5e38\u91cf\u8868\u8fbe\u5f0f\u51fd\u6570\u7684\u8981\u6c42\u4e5f\u662f\u4e0d\u786e\u5b9a\u7684\u3002<\/p>\n\n\n\n<p>\u9488\u5bf9\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0cC++11 \u6807\u51c6\u89c4\u5b9a\uff0c\u5982\u679c constexpr \u4fee\u9970\u7684\u6a21\u677f\u51fd\u6570\u5b9e\u4f8b\u5316\u7ed3\u679c\u4e0d\u6ee1\u8db3\u5e38\u91cf\u8868\u8fbe\u5f0f\u51fd\u6570\u7684\u8981\u6c42\uff0c\u5219 constexpr \u4f1a\u88ab\u81ea\u52a8\u5ffd\u7565\uff0c\u5373\u8be5\u51fd\u6570\u5c31\u7b49\u540c\u4e8e\u4e00\u4e2a\u666e\u901a\u51fd\u6570\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">constexpr\u548cconst\u7684\u533a\u522b\uff08\u91cd\u8981\uff09<\/h2>\n\n\n\n<p>\u6211\u4eec\u77e5\u9053\uff0cconstexpr \u662f C++ 11 \u6807\u51c6\u65b0\u6dfb\u52a0\u7684\u5173\u952e\u5b57\uff0c\u5728\u6b64\u4e4b\u524d\uff08C++ 98\/03\u6807\u51c6\uff09\u53ea\u6709 const \u5173\u952e\u5b57\uff0c\u5176\u5728\u5b9e\u9645\u4f7f\u7528\u4e2d\u7ecf\u5e38\u4f1a\u8868\u73b0\u51fa\u4e24\u79cd\u4e0d\u540c\u7684\u8bed\u4e49\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void dis_1(const int x){\n    \/\/\u9519\u8bef\uff0cx\u662f\u53ea\u8bfb\u7684\u53d8\u91cf\n    array &lt;int,x&gt; myarr{1,2,3,4,5};\n    cout &lt;&lt; myarr&#91;1] &lt;&lt; endl;\n}\n\nvoid dis_2(){\n    const int x = 5;\n    array &lt;int,x&gt; myarr{1,2,3,4,5};\n    cout &lt;&lt; myarr&#91;1] &lt;&lt; endl;\n}\n\nint main()\n{\n   dis_1(5);\n   dis_2();\n}<\/code><\/pre>\n\n\n\n<p>\u53ef\u4ee5\u770b\u5230\uff0cdis_1() \u548c dis_2() \u51fd\u6570\u4e2d\u90fd\u5305\u542b\u4e00\u4e2a const int x\uff0c\u4f46 dis_1() \u51fd\u6570\u4e2d\u7684 x \u65e0\u6cd5\u5b8c\u6210\u521d\u59cb\u5316 array \u5bb9\u5668\u7684\u4efb\u52a1\uff0c\u800c dis_2() \u51fd\u6570\u4e2d\u7684 x \u5374\u53ef\u4ee5\u3002<strong>\u8fd9\u662f\u56e0\u4e3a\uff0cdis_1() \u51fd\u6570\u4e2d\u7684\u201cconst int x\u201d\u53ea\u662f\u60f3\u5f3a\u8c03 x \u662f\u4e00\u4e2a\u53ea\u8bfb\u7684\u53d8\u91cf\uff0c\u5176\u672c\u8d28\u4ecd\u4e3a\u53d8\u91cf\uff0c\u65e0\u6cd5\u7528\u6765\u521d\u59cb\u5316 array \u5bb9\u5668<\/strong>\uff1b\u800c dis_2() \u51fd\u6570\u4e2d\u7684\u201cconst int x\u201d\uff0c\u8868\u660e x \u662f\u4e00\u4e2a\u53ea\u8bfb\u53d8\u91cf\u7684\u540c\u65f6\uff0cx \u8fd8\u662f\u4e00\u4e2a\u503c\u4e3a 5 \u7684\u5e38\u91cf\uff0c\u6240\u4ee5\u53ef\u4ee5\u7528\u6765\u521d\u59cb\u5316 array \u5bb9\u5668\u3002<\/p>\n\n\n\n<p>C++ 11\u6807\u51c6\u4e2d\uff0c\u4e3a\u4e86\u89e3\u51b3 const \u5173\u952e\u5b57\u7684\u53cc\u91cd\u8bed\u4e49\u95ee\u9898\uff0c<strong>\u4fdd\u7559\u4e86 const \u8868\u793a\u201c\u53ea\u8bfb\u201d\u7684\u8bed\u4e49\uff0c\u800c\u5c06\u201c\u5e38\u91cf\u201d\u7684\u8bed\u4e49\u5212\u5206\u7ed9\u4e86\u65b0\u6dfb\u52a0\u7684 constexpr \u5173\u952e\u5b57<\/strong>\u3002\u56e0\u6b64 C++11 \u6807\u51c6\u4e2d\uff0c\u5efa\u8bae\u5c06 const \u548c constexpr \u7684\u529f\u80fd\u533a\u5206\u5f00\uff0c\u5373\u51e1\u662f\u8868\u8fbe\u201c\u53ea\u8bfb\u201d\u8bed\u4e49\u7684\u573a\u666f\u90fd\u4f7f\u7528 const\uff0c\u8868\u8fbe\u201c\u5e38\u91cf\u201d\u8bed\u4e49\u7684\u573a\u666f\u90fd\u4f7f\u7528 constexpr\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u53ea\u8bfb\u548c\u4e0d\u53ef\u4fee\u6539\u6ca1\u6709\u5fc5\u7136\u5173\u8054<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>int main()\n{\n    int a = 10;\n    const int &amp; con_b = a;\n    cout &lt;&lt; con_b &lt;&lt; endl;\n\n    a = 20;\n    cout &lt;&lt; con_b &lt;&lt; endl;\n}<\/code><\/pre>\n\n\n\n<p>\u53ef\u4ee5\u770b\u5230\uff0c\u7a0b\u5e8f\u4e2d\u7528 const \u4fee\u9970\u4e86 con_b \u53d8\u91cf\uff0c\u8868\u793a\u8be5\u53d8\u91cf\u201c\u53ea\u8bfb\u201d\uff0c\u5373\u65e0\u6cd5\u901a\u8fc7\u53d8\u91cf\u81ea\u8eab\u53bb\u4fee\u6539\u81ea\u5df1\u7684\u503c\u3002\u4f46\u8fd9\u5e76\u4e0d\u610f\u5473\u7740 con_b \u7684\u503c\u4e0d\u80fd\u501f\u52a9\u5176\u5b83\u53d8\u91cf\u95f4\u63a5\u6539\u53d8\uff0c\u901a\u8fc7\u6539\u53d8 a \u7684\u503c\u5c31\u53ef\u4ee5\u4f7f con_b \u7684\u503c\u53d1\u751f\u53d8\u5316\u3002<\/p>\n\n\n\n<p>\u5728\u5927\u90e8\u5206\u5b9e\u9645\u573a\u666f\u4e2d\uff0cconst \u548c constexpr \u662f\u53ef\u4ee5\u6df7\u7528\u7684\u3002\u5b83\u4eec\u662f\u5b8c\u5168\u7b49\u4ef7\u7684\uff0c\u90fd\u53ef\u4ee5\u5728\u7a0b\u5e8f\u7684\u7f16\u8bd1\u9636\u6bb5\u8ba1\u7b97\u51fa\u7ed3\u679c\u3002\u4f46\u5728\u67d0\u4e9b\u573a\u666f\u4e2d\uff0c\u5fc5\u987b\u660e\u786e\u4f7f\u7528 constexpr\uff0c\u4f8b\u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>constexpr int sqr1(int arg){\n    return arg*arg;\n}\n\nconst int sqr2(int arg){\n    return arg*arg;\n}\n\nint main()\n{\n    array&lt;int,sqr1(10)&gt; mylist1;\/\/\u53ef\u4ee5\uff0c\u56e0\u4e3asqr1\u65f6constexpr\u51fd\u6570\n    array&lt;int,sqr2(10)&gt; mylist1;\/\/\u4e0d\u53ef\u4ee5\uff0c\u56e0\u4e3asqr2\u4e0d\u662fconstexpr\u51fd\u6570\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>\u5176\u4e2d\uff0c\u56e0\u4e3a sqr2() \u51fd\u6570\u7684\u8fd4\u56de\u503c\u4ec5\u6709 const \u4fee\u9970\uff0c\u800c\u6ca1\u6709\u7528\u66f4\u660e\u786e\u7684 constexpr \u4fee\u9970\uff0c\u5bfc\u81f4\u5176\u65e0\u6cd5\u7528\u4e8e\u521d\u59cb\u5316 array \u5bb9\u5668\uff08\u53ea\u6709\u5e38\u91cf\u624d\u80fd\u521d\u59cb\u5316 array \u5bb9\u5668\uff09\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u603b\u7ed3<\/h3>\n\n\n\n<p class=\"has-medium-font-size\"><strong>\u603b\u7684\u6765\u8bf4\u5728 C++ 11 \u6807\u51c6\u4e2d\uff0cconst \u7528\u4e8e\u4e3a\u4fee\u9970\u7684\u53d8\u91cf\u6dfb\u52a0\u201c\u53ea\u8bfb\u201d\u5c5e\u6027\uff1b\u800c constexpr \u5173\u952e\u5b57\u5219\u7528\u4e8e\u6307\u660e\u5176\u540e\u662f\u4e00\u4e2a\u5e38\u91cf\uff08\u6216\u8005\u5e38\u91cf\u8868\u8fbe\u5f0f\uff09\uff0c\u7f16\u8bd1\u5668\u5728\u7f16\u8bd1\u7a0b\u5e8f\u65f6\u53ef\u4ee5\u987a\u5e26\u5c06\u5176\u7ed3\u679c\u8ba1\u7b97\u51fa\u6765\uff0c\u800c\u65e0\u9700\u7b49\u5230\u7a0b\u5e8f\u8fd0\u884c\u9636\u6bb5\uff0c\u8fd9\u6837\u7684\u4f18\u5316\u6781\u5927\u5730\u63d0\u9ad8\u4e86\u7a0b\u5e8f\u7684\u6267\u884c\u6548\u7387\u3002<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8f6c\u81eaC\u8bed\u8a00\u4e2d\u6587\u7f51 http:\/\/c.biancheng.net\/view\/7781.html constexpr \u662f C++ 11 &#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-1995","post","type-post","status-publish","format-standard","hentry","category-c"],"_links":{"self":[{"href":"https:\/\/courtship.top\/index.php\/wp-json\/wp\/v2\/posts\/1995","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=1995"}],"version-history":[{"count":0,"href":"https:\/\/courtship.top\/index.php\/wp-json\/wp\/v2\/posts\/1995\/revisions"}],"wp:attachment":[{"href":"https:\/\/courtship.top\/index.php\/wp-json\/wp\/v2\/media?parent=1995"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/courtship.top\/index.php\/wp-json\/wp\/v2\/categories?post=1995"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/courtship.top\/index.php\/wp-json\/wp\/v2\/tags?post=1995"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}