{"id":262,"date":"2024-10-11T14:55:49","date_gmt":"2024-10-11T06:55:49","guid":{"rendered":"http:\/\/chenglixue.top\/?p=262"},"modified":"2024-10-11T14:58:50","modified_gmt":"2024-10-11T06:58:50","slug":"c-constexpr","status":"publish","type":"post","link":"http:\/\/chenglixue.top\/?p=262","title":{"rendered":"C++ constexpr"},"content":{"rendered":"<p><div class=\"has-toc have-toc\"><\/div><\/p>\n<h1>\u4ec0\u4e48\u662fconstexpr?<\/h1>\n<p>\u7cfb\u7edf\u5f88\u96be\u5206\u8fa8\u4e00\u4e2a\u521d\u59cb\u503c\u662f\u5426\u4e3a\u5e38\u91cf\u8868\u8fbe\u5f0f\uff0c\u4f46\u7528constexpr\u7c7b\u578b\u58f0\u660e\u7684\u53d8\u91cf\u53ef\u4ee5\u7531<strong>\u7f16\u8bd1\u5668\u6765\u9a8c\u8bc1\u53d8\u91cf\u7684\u503c\u662f\u5426\u662f\u4e00\u4e2a\u5e38\u91cf\u8868\u8fbe\u5f0f<\/strong>\uff0c\u4f7f\u5e38\u91cf\u8868\u8fbe\u5f0f\u5728\u7f16\u8bd1\u671f\u5c31\u53ef\u4ee5\u8ba1\u7b97\u5f97\u5230\u7ed3\u679c<\/p>\n<p>\u4ec0\u4e48\u662f\u5e38\u91cf\u8868\u8fbe\u5f0f\u5462\uff1f<strong>\u503c\u4e0d\u4f1a\u6539\u53d8\u4e14\u5728\u7f16\u8bd1\u65f6\u5c31\u53ef\u4ee5\u5f97\u5230\u8ba1\u7b97\u7ed3\u679c\u7684\u8868\u8fbe\u5f0f<\/strong>\u3002\u800c\u975e\u5e38\u91cf\u8868\u8fbe\u5f0f\u53ea\u80fd\u5728\u8fd0\u884c\u671f\u8ba1\u7b97\u51fa\u7ed3\u679c<\/p>\n<h1>\u4e3a\u4ec0\u4e48\u9700\u8981constexpr?<\/h1>\n<p>\u56e0\u4e3a\u5bf9\u4e8e\u5e38\u91cf\u8868\u8fbe\u5f0f\u53ef\u4ee5\u5728\u7f16\u8bd1\u9636\u6bb5\u8ba1\u7b97\u51fa\u7ed3\u679c\uff0c<strong>\u6781\u5927\u5730\u63d0\u9ad8\u6267\u884c\u6548\u7387<\/strong>\uff08\u8868\u8fbe\u5f0f\u5728\u7f16\u8bd1\u9636\u6bb5\u8ba1\u7b97\u51fa\u4e86\u7ed3\u679c\uff0c\u5c31\u53ef\u4ee5\u4e0d\u7528\u518d\u6bcf\u6b21\u8fd0\u884c\u65f6\u6765\u8ba1\u7b97\uff0c\u8fd9\u80af\u5b9a\u662f\u8282\u7ea6\u4e86\u65f6\u95f4\uff09<\/p>\n<h1>\u7279\u6027<\/h1>\n<ul>\n<li><strong>\u4f5c\u7528\u8303\u56f4<\/strong>\uff1a\u53ef\u4f5c\u7528\u4e8e\u53d8\u91cf\u3001\u51fd\u6570\uff08\u5305\u62ec\u6a21\u677f\u51fd\u6570\uff09\u3001class\u7684\u6784\u9020\u51fd\u6570<\/li>\n<li>\u5c06\u2f00\u4e2a<strong>\u51fd\u6570\u3001\u53d8\u91cf<\/strong>\u6807\u8bb0\u4e3aconstexpr\u7684\u540c\u65f6\uff0c\u4e5f\u5c06\u5b83\u6807\u8bb0\u4e3aconst\u3002\u4f46\u76f8\u53cd\u5e76\u4e0d\u6210\u2f74\uff0c\u2f00\u4e2aconst\u7684\u53d8\u91cf\u6216\u51fd\u6570\uff0c\u5e76\u4e0d\u662f constexpr\u7684<\/li>\n<li><strong>\u4e3a\u4e0d\u80fd\u4fee\u6539\u6570\u636e\u63d0\u4f9b\u4fdd\u969c<\/strong><\/li>\n<li>\u6709\u4e9b\u573a\u666f\uff0c\u7f16\u8bd1\u5668\u53ef\u4ee5\u5728\u7f16\u8bd1\u671f\u5bf9constexpr\u7684\u4ee3\u7801\u4f18\u5316\uff0c<strong>\u63d0\u9ad8\u6548\u7387<\/strong><\/li>\n<li>\u76f8\u2f50\u5b8f\u6765\u8bf4\uff0c\u6ca1\u6709\u989d\u5916\u7684\u5f00\u9500\uff0c\u4f46\u66f4<strong>\u5b89\u5168\u53ef\u9760<\/strong><\/li>\n<li><strong>\u5373\u4f7f\u4e00\u4e2a\u5e38\u91cf\u8868\u8fbe\u5f0f\u88abconstexpr\u4fee\u9970\uff0c\u62e5\u6709\u5728\u7f16\u8bd1\u9636\u6bb5\u8ba1\u7b97\u5f97\u51fa\u7ed3\u679c\u7684\u80fd\u529b\uff0c\u4f46\u8fd9\u5e76\u4e0d\u4ee3\u8868\u5b83\u4e00\u5b9a\u5c31\u80fd\u5728\u7f16\u8bd1\u9636\u6bb5\u88ab\u6267\u884c\uff0c\u662f\u5426\u6267\u884c\u7531\u7f16\u8bd1\u5668\u8bf4\u4e86\u7b97<\/strong><\/li>\n<\/ul>\n<h1>\u5f15\u7528<\/h1>\n<p><strong>constexpr\u6240\u5f15\u7528\u7684\u5bf9\u8c61\u5fc5\u987b\u5728\u7f16\u8bd1\u671f\u5c31\u51b3\u5b9a\u5730\u5740<\/strong><\/p>\n<pre><code class=\"line-numbers\">#include &lt;iostream&gt;\nusing namespace std;\n\nint g_tempA = 4;\nconst int g_conTempA = 4;\nconstexpr int g_conexprTempA = 4;\n\nint main(void)\n{\n    int tempA = 4;\n    const int conTempA = 4;\n    constexpr int conexprTempA = 4;\n    \/*1.\u6b63\u5e38\u8fd0\u884c\uff0c\u7f16\u8bd1\u901a\u8fc7*\/\n    const int &amp;conptrA = tempA;\n    const int &amp;conptrB = conTempA;\n    const int &amp;conptrC = conexprTempA;\n\n    \/*2.\u6709\u4e24\u4e2a\u95ee\u9898\uff1a\u4e00\u662f\u5f15\u7528\u5230\u5c40\u90e8\u53d8\u91cf\uff0c\u4e0d\u80fd\u518d\u7f16\u8bd1\u5668\u786e\u5b9a\uff1b\u4e8c\u662fconexprPtrB\u548cconexprPtrC\u5e94\u8be5\u4e3aconstexpr const\u7c7b\u578b\uff0c\u7f16\u8bd1\u4e0d\u8fc7*\/\n    constexpr int &amp;conexprPtrA = tempA;\n    constexpr int &amp;conexprPtrB = conTempA;\n    constexpr int &amp;conexprPtrC = conexprTempA;\n\n    \/*3.\u7b2c\u4e00\u4e2a\u7f16\u8bd1\u901a\u8fc7\uff0c\u540e\u4e24\u4e2a\u4e0d\u901a\u8fc7\uff0c\u539f\u56e0\u662f\u56e0\u4e3aconexprPtrE\u548cconexprPtrF\u5e94\u8be5\u4e3aconstexpr const\u7c7b\u578b*\/\n    constexpr int &amp;conexprPtrD = g_tempA;\n    constexpr int &amp;conexprPtrE = g_conTempA;\n    constexpr int &amp;conexprPtrF = g_conexprTempA;\n\n    \/*4.\u6b63\u5e38\u8fd0\u884c\uff0c\u7f16\u8bd1\u901a\u8fc7*\/\n    constexpr const int &amp;conexprConPtrD = g_tempA;\n    constexpr const int &amp;conexprConPtrE = g_conTempA;\n    constexpr const int &amp;conexprConPtrF = g_conexprTempA;\n\n    return 0;\n}\n<\/code><\/pre>\n<h1>\u6307\u9488<\/h1>\n<p>\u5c06constexpr\u4f5c\u7528\u4e8e\u6307\u9488\uff0c\u4f1a\u5c06\u6307\u9488\u8bbe\u5b9a\u4e3a<strong>\u6307\u9488\u5e38\u91cf<\/strong>\uff0c\u800c\u975e\u5e38\u91cf\u6307\u9488,\u4e5f\u5c31\u662f\u8bf4constexpr<strong>\u53ea\u5bf9\u6307\u9488\u751f\u6548\uff0c\u548c\u6240\u6307\u5bf9\u8c61\u65e0\u5173<\/strong><\/p>\n<pre><code class=\"line-numbers\">const int* p = nullptr;     \/\/p\u4e3a\u5e38\u91cf\u6307\u9488\nconstexpr int* q = nullptr; \/\/q\u4e3a\u6307\u9488\u5e38\u91cf\n<\/code><\/pre>\n<p><strong>constexpr\u6307\u9488\u4e0d\u53ef\u4ee5\u6307\u5411\u5b9a\u4e49\u5728\u51fd\u6570\u4f53\u5185\u7684\u5c40\u90e8\u53d8\u91cf<\/strong>\uff0c\u56e0\u4e3a\u8fd9\u4e9b\u53d8\u91cf\u5e76\u975e\u5b58\u653e\u5728\u56fa\u5b9a\u5730\u5740\uff0c\u8fdb\u5165\u51fd\u6570\u4f5c\u7528\u57df\u4f1a\u521d\u59cb\u5316\u5b83\u4eec\uff0c\u79bb\u5f00\u51fd\u6570\u4f5c\u7528\u57df\u5219\u9500\u6bc1\u5b83\u4eec;\u76f8\u5bf9\u7684\uff0c\u5bf9\u4e8e\u90a3\u4e9b\u5b9a\u4e49\u5728\u51fd\u6570\u4f53\u4e4b\u5916\u7684\u53d8\u91cf\u548c\u5c40\u90e8\u9759\u6001\u5bf9\u8c61\uff0c\u56e0\u4e3a\u5176\u751f\u547d\u5468\u671f\u662f\u4ece\u521d\u59cb\u5316\u5230\u7a0b\u5e8f\u7ec8\u6b62\uff0c\u5176\u5730\u5740\u5e76\u4e0d\u4f1a\u6539\u53d8\uff0c\u6240\u4ee5constexpr\u6307\u9488\u53ef\u4ee5\u6307\u5411\u4ed6\u4eec<\/p>\n<h1>\u51fd\u6570<\/h1>\n<p>constexpr\u51fd\u6570\u5f53\u9700\u8981\u4f7f\u7528\u5b83\u7684\u4ee3\u7801\u65f6\uff0c\u5728\u7f16\u8bd1\u671f\u8ba1\u7b97\u5176\u8fd4\u56de\u503c<\/p>\n<p>\u89c4\u5219\uff1a<\/p>\n<ol>\n<li>\u53ea\u63a5\u53d7\u5e76\u8fd4\u56de\u6587\u672c\u7c7b\u578b,\u8fd4\u56de\u503c\u4e0d\u80fd\u662fvoid<\/li>\n<li>\u51fd\u6570\u4f53\u4e0d\u80fd\u58f0\u660e\u53d8\u91cf\u6216\u5b9a\u4e49\u65b0\u7684\u7c7b\u578b<\/li>\n<li>\u51fd\u6570\u7684\u8fd4\u56de\u7c7b\u578b\u548c\u5f62\u53c2\u7c7b\u578b\u90fd\u5fc5\u987b\u4e3a\u5b57\u9762\u503c\u7c7b\u578b<\/li>\n<li>\u53ef\u4ee5\u662f\u9012\u5f52\u7684<\/li>\n<li>\u6b63\u6587\u4e0d\u80fd\u5305\u542b\u4efb\u4f55 <strong>goto<\/strong> \u8bed\u53e5\u6216 <strong>try<\/strong> \u5757<\/li>\n<\/ol>\n<p>\u8981\u60f3constexpr\u80fd\u5728\u7f16\u8bd1\u671f\u901a\u8fc7\uff0c<strong>\u51fd\u6570\u7684\u8fd4\u56de\u7c7b\u578b\u548c\u5f62\u53c2\u7c7b\u578b\u90fd\u5fc5\u987b\u4e3a\u5b57\u9762\u503c\u7c7b\u578b,\u4e14\u51fd\u6570\u4e2d\u6709\u4e14\u53ea\u6709\u4e00\u6761return\u8bed\u53e5,\u51fd\u6570\u4f53\u5185\u53ef\u4ee5\u6709\u5176\u4ed6\u8bed\u53e5\uff0c\u4f46\u53ea\u80fd\u5305\u542b\u5728\u8fd0\u884c\u671f\u4e0d\u6267\u884c\u4efb\u4f55\u64cd\u4f5c\u7684\uff0c\u53ef\u4ee5\u5305\u542b using \u6307\u4ee4\u3001typedef \u8bed\u53e5\u4ee5\u53ca static_assert \u65ad\u8a00\u3001\u7a7a\u8bed\u53e5\u3001null\u8bed\u53e5<\/strong><\/p>\n<p>constexpr\u51fd\u6570\u53ef\u4ee5\u591a\u6b21\u5b9a\u4e49\uff0c<strong>\u4e00\u822c\u5b9a\u4e49\u5728\u5934\u6587\u4ef6\u4e2d<\/strong><\/p>\n<pre><code class=\"line-numbers\">constexpr int func() { return 1; }\nconstexpr int a = func();   \/\/a\u4e3a\u5e38\u91cf\u8868\u8fbe\u5f0f\n\n\/\/\u5f53\u6211\u4eec\u4e3a\u5982\u4e0b\u5b9e\u4f8b\u7684func1\u4f20\u5165\u4e3a\u5e38\u91cf\u8868\u8fbe\u5f0f\u7684\u5b9e\u53c2\u65f6\uff0c\u51fd\u6570\u7684\u8fd4\u56de\u503c\u4e3a\u5e38\u91cf\u8868\u8fbe\u5f0f;\u82e5\u975e\u5e38\u91cf\u8868\u8fbe\u5f0f\uff0c\u5219\u8fd4\u56de\u503c\u4e5f\u662f\u975e\u5e38\u91cf\u8868\u8fbe\u5f0f\nconstexpr func1( size_t s ) { return a * s; }\nint arr[func1(2)];  \/\/\u5bf9\uff0cfunc1(2)\u4e3a\u5e38\u91cf\u8868\u8fbe\u5f0f\nint i = 1;\nint arr[func1(i)];  \/\/\u9519\uff0cfunc1(i)\u4e0d\u662f\u5e38\u91cf\u8868\u8fbe\u5f0f\n<\/code><\/pre>\n<h1>class<\/h1>\n<p>\u6784\u9020\u51fd\u6570\u4e0d\u80fd\u4e3aconst\uff0c\u4f46<strong>\u5b57\u9762\u503c\u5e38\u91cf\u7c7b\u7684\u6784\u9020\u51fd\u6570\u53ef\u4ee5\u4e3aconstexpr<\/strong><\/p>\n<p>constexpr\u6784\u9020\u51fd\u6570\u9650\u5236\uff1a<\/p>\n<ol>\n<li>\u4e0d\u80fd\u662f\u865a\u51fd\u6570<\/li>\n<li>\u4e3b\u4f53\u53ef\u4ee5\u5b9a\u4e49\u4e3a default \u6216 delete\u3002\u5426\u5219\u5fc5\u987b\u65e2\u7b26\u5408\u6784\u9020\u51fd\u6570\u7684\u8981\u6c42\u4e0d\u80fd\u5305\u542breturn\u8bed\u53e5\uff0c\u53c8\u7b26\u5408constexpr\u51fd\u6570\u7684\u8981\u6c42\uff0c\u4e5f\u5c31\u662f\u8bf4constexpr\u6784\u9020\u51fd\u6570\u4f53\u4e00\u822c\u4e3a\u7a7a<\/li>\n<li>\u5fc5\u987b\u521d\u59cb\u5316\u6240\u6709\u6570\u636e\u6210\u5458\uff0c\u521d\u59cb\u503c\u6216\u8005\u4f7f\u7528constexpr\u6784\u9020\u51fd\u6570\uff0c\u4ea6\u6216\u662f\u5e38\u91cf\u8868\u8fbe\u5f0f<\/li>\n<li>\u6ee1\u8db3constexpr\u51fd\u6570\u89c4\u5219<\/li>\n<\/ol>\n<pre><code class=\"line-numbers\">#include &lt;iostream&gt;\nusing namespace std;\n\nclass Test \n{\npublic:\n    constexpr Test(int num1, int num2) : m_num1(num1), m_num2(num2)\n    {\n\n    }\n\npublic:\n    int m_num1;\n    int m_num2;\n};\n\nint main(void) \n{\n    constexpr Test t1(1, 2);\n\n    enum e\n    { \n        x = t1.m_num1, \n        y = t1.m_num2 \n    };\n\n    return 0;\n}\n<\/code><\/pre>\n<h1>Reference<\/h1>\n<p><a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/blog.csdn.net\/yao_hou\/article\/details\/109301290\" target=\"_blank\"  rel=\"nofollow\" >C++11\u5173\u952e\u5b57constexpr\u770b\u8fd9\u7bc7\u5c31\u591f\u4e86<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4ec0\u4e48\u662fconstexpr? \u7cfb\u7edf\u5f88\u96be\u5206\u8fa8\u4e00\u4e2a\u521d\u59cb\u503c\u662f\u5426\u4e3a\u5e38\u91cf\u8868\u8fbe\u5f0f\uff0c\u4f46\u7528constexpr\u7c7b\u578b\u58f0\u660e\u7684\u53d8\u91cf\u53ef\u4ee5\u7531\u7f16\u8bd1\u5668\u6765\u9a8c\u8bc1\u53d8\u91cf\u7684\u503c &#8230;<\/p>","protected":false},"author":1,"featured_media":231,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"_links":{"self":[{"href":"http:\/\/chenglixue.top\/index.php?rest_route=\/wp\/v2\/posts\/262"}],"collection":[{"href":"http:\/\/chenglixue.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/chenglixue.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/chenglixue.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/chenglixue.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=262"}],"version-history":[{"count":2,"href":"http:\/\/chenglixue.top\/index.php?rest_route=\/wp\/v2\/posts\/262\/revisions"}],"predecessor-version":[{"id":264,"href":"http:\/\/chenglixue.top\/index.php?rest_route=\/wp\/v2\/posts\/262\/revisions\/264"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/chenglixue.top\/index.php?rest_route=\/wp\/v2\/media\/231"}],"wp:attachment":[{"href":"http:\/\/chenglixue.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=262"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/chenglixue.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=262"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/chenglixue.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=262"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}