{"id":459,"date":"2025-09-23T23:23:58","date_gmt":"2025-09-23T15:23:58","guid":{"rendered":"http:\/\/yangnanbei.top\/?p=459"},"modified":"2025-09-23T23:23:58","modified_gmt":"2025-09-23T15:23:58","slug":"%e5%b7%a6%e5%80%bc%e5%92%8c%e5%8f%b3%e5%80%bc%ef%bc%8c%e7%a7%bb%e5%8a%a8%e8%af%ad%e4%b9%89%e4%b8%8e%e5%ae%8c%e7%be%8e%e8%bd%ac%e5%8f%91","status":"publish","type":"post","link":"http:\/\/yangnanbei.top\/index.php\/2025\/09\/23\/%e5%b7%a6%e5%80%bc%e5%92%8c%e5%8f%b3%e5%80%bc%ef%bc%8c%e7%a7%bb%e5%8a%a8%e8%af%ad%e4%b9%89%e4%b8%8e%e5%ae%8c%e7%be%8e%e8%bd%ac%e5%8f%91\/","title":{"rendered":"\u5de6\u503c\u548c\u53f3\u503c\uff0c\u79fb\u52a8\u8bed\u4e49\u4e0e\u5b8c\u7f8e\u8f6c\u53d1"},"content":{"rendered":"\n<h1 class=\"wp-block-heading\">\u4ec0\u4e48\u662f\u5de6\u503c\uff0c\u4ec0\u4e48\u662f\u53f3\u503c\uff1f<\/h1>\n\n\n\n<p>\u5de6\u503c\uff1a\u5177\u6709\u751f\u547d\u5468\u671f\u7684\uff0c\u5177\u6709\u540d\u79f0\u7684\u503c\uff0c\u5373\u4e3a\u5b83\u6709\u4e00\u4e2a\u5177\u4f53\u7684\u5185\u5b58\u7a7a\u95f4\u3002<\/p>\n\n\n\n<p>\u53f3\u503c\uff1a\u6ca1\u6709\u751f\u547d\u5468\u671f\uff0c\u4e5f\u6210\u4e3a\u5c06\u4ea1\u503c\u3002\u4e0d\u6307\u5411\u7a33\u5b9a\u5185\u5b58\u5730\u5740\u7684\u533f\u540d\u503c\u3002<\/p>\n\n\n\n<p>\u57fa\u4e8e\u4e0a\u8ff0\u7279\u6027\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u7528\u53d6\u5730\u5740\u7b26\u53f7\u5224\u65ad\uff0c\u80fd\u591f\u53d6\u5230\u5730\u5740\u7684\u662f\u5de6\u503c\uff0c\u4e0d\u80fd\u53d6\u5230\u7684\u662f\u53f3\u503c\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u5de6\u503c\u4e0e\u53f3\u503c\u7684\u7406\u89e3<\/h2>\n\n\n\n<p>\u4ece\u5b57\u9762\u7406\u89e3\uff0c\u65e0\u975e\u662f\u8868\u8fbe\u5f0f\u7b49\u53f7\u5de6\u8fb9\u7684\u503c\u4e3a\u5de6\u503c\uff0c\u8868\u8fbe\u5f0f\u53f3\u8fb9\u7684\u503c\u4e3a\u53f3\u503c\u3002<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#f6f6f4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#282A36\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#f6f6f4;display:none\" aria-label=\"\u590d\u5236\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>int x = 1;\nint y = 2;\nint z = x + y;<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki dracula-soft\" style=\"background-color: #282A36\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F286C4\">int<\/span><span style=\"color: #F6F6F4\"> x <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">1<\/span><span style=\"color: #F6F6F4\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F286C4\">int<\/span><span style=\"color: #F6F6F4\"> y <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">2<\/span><span style=\"color: #F6F6F4\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F286C4\">int<\/span><span style=\"color: #F6F6F4\"> z <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> x <\/span><span style=\"color: #F286C4\">+<\/span><span style=\"color: #F6F6F4\"> y;<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>\u4ee5\u4e0a\u8ff0\u4ee3\u7801\u4e3a\u4f8b\u5b50\uff0cx\u662f\u5de6\u503c\uff0c1\u662f\u53f3\u503c\uff1by\u662f\u5de6\u503c\uff0c2\u662f\u53f3\u503c\u3002<\/p>\n\n\n\n<p>z\u662f\u5de6\u503c\uff0cx+y\u7684\u7ed3\u679c\u662f\u53f3\u503c\u3002<\/p>\n\n\n\n<p>\u4f46\u662f\u7b80\u5355\u5730\u7528\u8868\u8fbe\u5f0f\u5de6\u53f3\u6765\u533a\u5206\u5de6\u53f3\u503c\u4ecd\u7136\u8fc7\u4e8e\u6b66\u65ad\uff0c\u8ba9\u6211\u4eec\u8003\u8651\u4e0b\u9762\u7684\u4f8b\u5b50<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#f6f6f4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#282A36\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#f6f6f4;display:none\" aria-label=\"\u590d\u5236\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>int a = 1;\nint b = a;<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki dracula-soft\" style=\"background-color: #282A36\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F286C4\">int<\/span><span style=\"color: #F6F6F4\"> a <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">1<\/span><span style=\"color: #F6F6F4\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F286C4\">int<\/span><span style=\"color: #F6F6F4\"> b <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> a;<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>\u5728line1 a\u663e\u7136\u662f\u5de6\u503c\uff0c\u4f46\u662fline2\u4e2da\u5374\u53d8\u6210\u4e86\u53f3\u503c\u3002\u6545\u800c\u4ee5\u8868\u8fbe\u5f0f\u533a\u5206\u662f\u6709\u77db\u76fe\u7684\u3002\u6240\u4ee5\u6211\u4eec\u8fd8\u662f\u5e94\u8be5\u7406\u89e3\u5176\u5185\u5728\u542b\u4e49\u3002<\/p>\n\n\n\n<p>\u5728c++\u4e2d <code>lvalue<\/code> \u4e00\u822c\u8868\u793a<strong>\u6307\u5411\u4e00\u4e2a\u5177\u4f53\u5730\u5740(\u5177\u4f53<\/strong><strong>\u5185\u5b58<\/strong><strong>)\u7684\u5177\u6709\u540d\u79f0\u7684\u503c<\/strong>\uff08\u5177\u540d\u5bf9\u8c61\uff09\u3002\u5b83\u6709\u4e00\u4e2a\u76f8\u5bf9\u7a33\u5b9a\u7684\u5185\u5b58\u5730\u5740\uff0c\u5e76\u4e14\u6709\u4e00\u6bb5\u8f83\u957f\u7684\u751f\u547d\u5468\u671f\u3002\u800c\u53f3\u503c\u5219\u662f<strong>\u4e0d\u6307\u5411\u7a33\u5b9a\u5185\u5b58\u5730\u5740\u7684\u533f\u540d\u503c<\/strong>\uff08\u4e0d\u5177\u540d\u5bf9\u8c61\uff09\u3002\u4ee5\u4e0a\u8ff0\u4ee3\u7801\u4e3a\u4f8b\u5b50\uff0c\u56e0\u4e3a&amp;a\u548c&amp;b\u90fd\u80fd\u591f\u53d6\u5230\u5730\u5740\uff0c\u6240\u4ee5a\u548cb\u90fd\u662f\u5de6\u503c\u3002<\/p>\n\n\n\n<p>\u8ba9\u6211\u4eec\u8003\u8651\u4ee5\u4e0b\u4ee3\u7801<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#f6f6f4;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#282A36\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#f6f6f4;display:none\" aria-label=\"\u590d\u5236\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>#include &lt;iostream>\n\nusing namespace std;\n\nint x = 1;\n\nint get_val()\n{\n    return x;\n}\n\nvoid set_val(int val)\n{\n    x = val;\n}\n\nint main() {\n    x++; \/* rvalue *\/\n    ++x; \/* lvalue *\/\n\n    int *q = &amp;++x; \/* success *\/\n    int *p = &amp;x++; \/* fail *\/\n    int *r = &amp;get_val(); \/* fail *\/\n    \n    int y = get_val();\n    set_val(6);\n    auto p = &amp;\"this is a lvalue\";\n}<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki dracula-soft\" style=\"background-color: #282A36\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F286C4\">#include<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #DEE492\">&lt;<\/span><span style=\"color: #E7EE98\">iostream<\/span><span style=\"color: #DEE492\">&gt;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F286C4\">using<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #F286C4\">namespace<\/span><span style=\"color: #F6F6F4\"> std;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F286C4\">int<\/span><span style=\"color: #F6F6F4\"> x <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">1<\/span><span style=\"color: #F6F6F4\">;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F286C4\">int<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #62E884\">get_val<\/span><span style=\"color: #F6F6F4\">()<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    <\/span><span style=\"color: #F286C4\">return<\/span><span style=\"color: #F6F6F4\"> x;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">}<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F286C4\">void<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #62E884\">set_val<\/span><span style=\"color: #F6F6F4\">(<\/span><span style=\"color: #F286C4\">int<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #FFB86C; font-style: italic\">val<\/span><span style=\"color: #F6F6F4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    x <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> val;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">}<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F286C4\">int<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #62E884\">main<\/span><span style=\"color: #F6F6F4\">() {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    x<\/span><span style=\"color: #F286C4\">++<\/span><span style=\"color: #F6F6F4\">;<\/span><span style=\"color: #7B7F8B\"> \/* rvalue *\/<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    <\/span><span style=\"color: #F286C4\">++<\/span><span style=\"color: #F6F6F4\">x;<\/span><span style=\"color: #7B7F8B\"> \/* lvalue *\/<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    <\/span><span style=\"color: #F286C4\">int<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #F286C4\">*<\/span><span style=\"color: #F6F6F4\">q <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #F286C4\">&amp;++<\/span><span style=\"color: #F6F6F4\">x;<\/span><span style=\"color: #7B7F8B\"> \/* success *\/<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    <\/span><span style=\"color: #F286C4\">int<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #F286C4\">*<\/span><span style=\"color: #F6F6F4\">p <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #F286C4\">&amp;<\/span><span style=\"color: #F6F6F4\">x<\/span><span style=\"color: #F286C4\">++<\/span><span style=\"color: #F6F6F4\">;<\/span><span style=\"color: #7B7F8B\"> \/* fail *\/<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    <\/span><span style=\"color: #F286C4\">int<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #F286C4\">*<\/span><span style=\"color: #F6F6F4\">r <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #F286C4\">&amp;<\/span><span style=\"color: #62E884\">get_val<\/span><span style=\"color: #F6F6F4\">();<\/span><span style=\"color: #7B7F8B\"> \/* fail *\/<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    <\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    <\/span><span style=\"color: #F286C4\">int<\/span><span style=\"color: #F6F6F4\"> y <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #62E884\">get_val<\/span><span style=\"color: #F6F6F4\">();<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    <\/span><span style=\"color: #62E884\">set_val<\/span><span style=\"color: #F6F6F4\">(<\/span><span style=\"color: #BF9EEE\">6<\/span><span style=\"color: #F6F6F4\">);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    <\/span><span style=\"color: #F286C4\">auto<\/span><span style=\"color: #F6F6F4\"> p <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #F286C4\">&amp;<\/span><span style=\"color: #DEE492\">&quot;<\/span><span style=\"color: #E7EE98\">this is a lvalue<\/span><span style=\"color: #DEE492\">&quot;<\/span><span style=\"color: #F6F6F4\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">}<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>\u5728main\u51fd\u6570\u4e2d++x\u548cx++\u867d\u7136\u90fd\u662f\u5bf9x\u505a\u81ea\u589e\u64cd\u4f5c\uff0c\u4f46\u662f\u5b9e\u9645\u4e0a++x\u662f\u5bf9x\u81ea\u589e\u540e\u9a6c\u4e0a\u8fd4\u56de\u5176\u81ea\u8eab\uff0c\u800cx++\u662f\u5148\u751f\u6210x\u7684\u4e34\u65f6\u590d\u5236\u7136\u540e\u624d\u5bf9x\u9012\u589e\uff0c\u6700\u540e\u8fd4\u56de\u4e34\u65f6\u590d\u5236\u5185\u5bb9\u3002<\/p>\n\n\n\n<p>\u6240\u4ee5\u6211\u4eec\u770b\u4e0a\u8ff0\u53d6\u5730\u5740\u64cd\u4f5c\uff0cx++\u65e0\u6cd5\u53d6\u5230\u5730\u5740\uff0c\u800c++x\u53ef\u4ee5\u3002<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>int *q = &amp;++x; \/* success *\/<\/p>\n\n\n\n<p>int *p = &amp;x++; \/* fail *\/<\/p>\n<\/blockquote>\n\n\n\n<p>\u7f16\u8bd1\u5668\u544a\u8b66<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>test1.cpp:20:20: error: lvalue required as unary \u2018&amp;\u2019 operand<\/p>\n\n\n\n<p>20 | int <em>p = &amp;x++; \/<\/em> fail *\/<\/p>\n<\/blockquote>\n\n\n\n<p>\u6211\u4eec\u518d\u770bget_val\u8fd9\u4e2a\u51fd\u6570\uff0c\u5b83\u8fd4\u56de\u7684\u662f\u5de6\u503c\u8fd8\u662f\u53f3\u503c\u5462\uff1f\u5b83\u867d\u7136\u8fd4\u56de\u4e86\u4e00\u4e2a\u5168\u5c40\u53d8\u91cfx\uff0c\u4f46\u662f\u5b9e\u9645\u4e0a\u8fd4\u56de\u7684\u662fx\u7684\u4e34\u65f6\u590d\u5236\uff0c\u6545\u800c\u662f\u4e00\u4e2a\u53f3\u503c\u3002\u6240\u4ee5 int *r = &amp;get_val() \u4e5f\u4f1a\u5931\u8d25\u3002<\/p>\n\n\n\n<p>\u5728main\u51fd\u6570 set_val\u4e2d\u5b9e\u53c26\u662f\u4e00\u4e2a\u53f3\u503c\uff0c\u4f46\u662f\u8fdb\u5165\u51fd\u6570\u4e4b\u540e\u5f62\u53c2val\u5374\u662f\u4e00\u4e2a\u5de6\u503c\u3002\u61c2\u5f97\u51fd\u6570\u538b\u6808\u884c\u4e3a\u7684\u540c\u5b66\u53ef\u80fd\u77e5\u9053val\u662f\u4f1a\u88ab\u538b\u5165\u5bc4\u5b58\u5668\uff0c\u6240\u4ee5\u6b64\u65f6\u82e5\u5bf9\u5176\u53d6\u5730\u5740\u53d6\u5230\u7684\u662f\u5bc4\u5b58\u5668\u5730\u5740\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u524d\u7f6e++\u548c\u540e\u7f6e++\u7684\u5b9e\u73b0<\/h2>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#f6f6f4;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#282A36\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#f6f6f4;display:none\" aria-label=\"\u590d\u5236\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>\/* \u524d\u7f6e++ *\/\nInteger&amp; operator++() {\n    ++value;\n    return *this;\n}\n\n\/* \u540e\u7f6e++ *\/\nInteger operator++(int) {\n    Integer tmp = *this;\n    ++value;\n    return tmp;\n}\n<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki dracula-soft\" style=\"background-color: #282A36\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #7B7F8B\">\/* \u524d\u7f6e++ *\/<\/span><\/span>\n<span class=\"line\"><span style=\"color: #97E1F1; font-style: italic\">Integer<\/span><span style=\"color: #F286C4\">&amp;<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #F286C4\">operator<\/span><span style=\"color: #F6F6F4\">++() {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    <\/span><span style=\"color: #F286C4\">++<\/span><span style=\"color: #F6F6F4\">value;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    <\/span><span style=\"color: #F286C4\">return<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #F286C4\">*<\/span><span style=\"color: #BF9EEE; font-style: italic\">this<\/span><span style=\"color: #F6F6F4\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">}<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #7B7F8B\">\/* \u540e\u7f6e++ *\/<\/span><\/span>\n<span class=\"line\"><span style=\"color: #97E1F1; font-style: italic\">Integer<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #F286C4\">operator<\/span><span style=\"color: #F6F6F4\">++(<\/span><span style=\"color: #F286C4\">int<\/span><span style=\"color: #F6F6F4\">) {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    Integer tmp <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #F286C4\">*<\/span><span style=\"color: #BF9EEE; font-style: italic\">this<\/span><span style=\"color: #F6F6F4\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    <\/span><span style=\"color: #F286C4\">++<\/span><span style=\"color: #F6F6F4\">value;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    <\/span><span style=\"color: #F286C4\">return<\/span><span style=\"color: #F6F6F4\"> tmp;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">}<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<h1 class=\"wp-block-heading\">\u4ec0\u4e48\u662f\u5de6\u503c\u5f15\u7528\uff1f\u4ec0\u4e48\u662f\u53f3\u503c\u5f15\u7528\uff1f<\/h1>\n\n\n\n<h2 class=\"wp-block-heading\">\u6982\u5ff5<\/h2>\n\n\n\n<h2 class=\"wp-block-heading\">\u5de6\u503c\u5f15\u7528<\/h2>\n\n\n\n<p>\u5de6\u503c\u5f15\u7528\uff1a\u5de6\u503c\u5f15\u7528\u662f\u6307\u5411\u5de6\u503c\u7684\u5f15\u7528\u3002\u5de6\u503c\u5f15\u7528\u53ea\u80fd\u7528\u5355\u4e2a\u7b26\u53f7&amp;\u8868\u793a\u3002<strong>\u5e38\u91cf\u5de6\u503c\u5f15\u7528<\/strong>\u53ef\u4ee5\u7ed1\u5b9a\u5de6\u503c\uff0c\u4e5f\u53ef\u4ee5\u7ed1\u5b9a\u53f3\u503c\uff0c<strong>\u975e\u5e38\u91cf\u5de6\u503c\u5f15\u7528\u53ea\u80fd\u7ed1\u5b9a\u5de6\u503c.<\/strong><\/p>\n\n\n\n<p>\u8fd9\u6837\u8bf4\u8d77\u6765\u6709\u70b9\u7ed5\uff0c\u6211\u4eec\u6765\u770b\u4e00\u4e2a\u4f8b\u5b50<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#f6f6f4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#282A36\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#f6f6f4;display:none\" aria-label=\"\u590d\u5236\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>int &amp;x1 = 1; \/\/ compile err\nconst int &amp;x2 = 11; \/\/ compile succ<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki dracula-soft\" style=\"background-color: #282A36\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F286C4\">int<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #F286C4\">&amp;<\/span><span style=\"color: #F6F6F4\">x1 <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">1<\/span><span style=\"color: #F6F6F4\">;<\/span><span style=\"color: #7B7F8B\"> \/\/ compile err<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F286C4\">const<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #F286C4\">int<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #F286C4\">&amp;<\/span><span style=\"color: #F6F6F4\">x2 <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">11<\/span><span style=\"color: #F6F6F4\">;<\/span><span style=\"color: #7B7F8B\"> \/\/ compile succ<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>\u7f16\u8bd1\u9519\u8bef:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>error: cannot bind non-const lvalue reference of type \u2018int&amp;\u2019 to an rvalue of type \u2018int\u2019<\/p>\n\n\n\n<p>29 | int &amp;x1 = 1; \/\/ compile err<\/p>\n<\/blockquote>\n\n\n\n<p><code>int &amp;x1 <\/code>\u662f\u975e\u5e38\u91cf\u5de6\u503c\u5f15\u7528\uff0c1\u662f\u53f3\u503c\uff0c\u975e\u5e38\u91cf\u5de6\u503c\u5f15\u7528\u53ea\u80fd\u7ed1\u5b9a\u5de6\u503c\u30021\u662f\u53f3\u503c\uff0c\u6240\u4ee5\u6b64\u65f6\u62a5\u9519<\/p>\n\n\n\n<p><code>const int &amp;x2<\/code> \u662f\u5e38\u91cf\u5de6\u503c\u5f15\u7528\uff0c\u53ef\u4ee5\u7ed1\u5b9a\u5de6\u503c\u548c\u53f3\u503c\u3002<\/p>\n\n\n\n<p>\u90a3\u4e48\u8fd9\u4e2a\u7279\u6027\u6709\u4ec0\u4e48\u4f5c\u7528\u5462\uff1f\u5728\u51fd\u6570\u5f62\u53c2\u5217\u8868\u4e2d\u5177\u6709\u91cd\u5927\u610f\u4e49\uff0c\u6bd4\u5982\u5728\u590d\u5236\u6784\u9020\u51fd\u6570\u548c\u590d\u5236\u8fd0\u7b97\u7b26\u51fd\u6570\u4e2d\uff0c\u901a\u5e38\u60c5\u51b5\u4e0b\u6211\u4eec\u5b9e\u73b0\u8fd9\u4e24\u4e2a\u51fd\u6570\u7684\u5f62\u53c2\u90fd\u662f\u4e00\u4e2a\u5e38\u91cf\u5de6\u503c\u5f15\u7528\u3002<\/p>\n\n\n\n<p>\u8003\u8651\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#f6f6f4;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#282A36\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#f6f6f4;display:none\" aria-label=\"\u590d\u5236\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>class X {\npublic:\n    X() {}\n    X(const X&amp;) {}\n    X&amp; operator = (const X&amp;) { return *this; }\n};\n\nX make_x()\n{\n    return X();\n}\n\nint main()\n{\n    X x1;\n    X x2(x1);\n    X x3(make_x());\n    x3 = make_x();\n}<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki dracula-soft\" style=\"background-color: #282A36\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F286C4\">class<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #97E1F1\">X<\/span><span style=\"color: #F6F6F4\"> {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F286C4\">public:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    <\/span><span style=\"color: #62E884\">X<\/span><span style=\"color: #F6F6F4\">() {}<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    <\/span><span style=\"color: #62E884\">X<\/span><span style=\"color: #F6F6F4\">(<\/span><span style=\"color: #F286C4\">const<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #97E1F1; font-style: italic\">X<\/span><span style=\"color: #F286C4\">&amp;<\/span><span style=\"color: #F6F6F4\">) {}<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    <\/span><span style=\"color: #97E1F1; font-style: italic\">X<\/span><span style=\"color: #F286C4\">&amp;<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #F286C4\">operator<\/span><span style=\"color: #F6F6F4\"> = (<\/span><span style=\"color: #F286C4\">const<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #97E1F1; font-style: italic\">X<\/span><span style=\"color: #F286C4\">&amp;<\/span><span style=\"color: #F6F6F4\">) { <\/span><span style=\"color: #F286C4\">return<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #F286C4\">*<\/span><span style=\"color: #BF9EEE; font-style: italic\">this<\/span><span style=\"color: #F6F6F4\">; }<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">};<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #97E1F1; font-style: italic\">X<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #62E884\">make_x<\/span><span style=\"color: #F6F6F4\">()<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    <\/span><span style=\"color: #F286C4\">return<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #62E884\">X<\/span><span style=\"color: #F6F6F4\">();<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">}<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F286C4\">int<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #62E884\">main<\/span><span style=\"color: #F6F6F4\">()<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    X x1;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    X <\/span><span style=\"color: #62E884\">x2<\/span><span style=\"color: #F6F6F4\">(x1);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    X <\/span><span style=\"color: #62E884\">x3<\/span><span style=\"color: #F6F6F4\">(<\/span><span style=\"color: #62E884\">make_x<\/span><span style=\"color: #F6F6F4\">());<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    x3 <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #62E884\">make_x<\/span><span style=\"color: #F6F6F4\">();<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">}<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>\u4ee5\u4e0a\u4ee3\u7801\u53ef\u4ee5\u7f16\u8bd1\u901a\u8fc7\uff0c\u4f46\u662f\u8ba9\u6211\u4eec\u6d4b\u8bd5\u4e00\u4e0b\uff0c<\/p>\n\n\n\n<p>\u5982\u679c\u53bb\u9664\u590d\u5236\u6784\u9020\u51fd\u6570\u4e2d\u7684const\uff0c\u5219 X x3(make_x()); \u548c x3 = make_x(); \u4f1a\u62a5\u9519\uff0c\u56e0\u4e3a\u975e\u5e38\u91cf\u5de6\u503c\u5f15\u7528\u65e0\u6cd5\u7ed1\u5b9a\u5230X make_x() \u4ea7\u751f\u7684\u53f3\u503c\u3002<\/p>\n\n\n\n<p>\u4f46\u662f\u5b83\u4e5f\u5b58\u5728\u4e00\u4e2a\u5f88\u5927\u7684<strong>\u7f3a\u70b9<\/strong>\u2014\u2014\u5e38\u91cf\u6027\u3002\u4e00\u65e6\u4f7f\u7528\u4e86\u5e38\u91cf\u5de6\u503c\u5f15\u7528\uff0c\u5c31\u8868\u793a\u6211\u4eec\u65e0\u6cd5\u5728\u51fd\u6570\u5185\u4fee\u6539\u8be5\u5bf9\u8c61\u7684\u5185\u5bb9\uff08\u5f3a\u5236\u7c7b\u578b\u8f6c\u6362 \u9664\u5916\uff09\u3002\u6240\u4ee5\u9700\u8981\u53e6\u5916\u4e00\u4e2a\u7279\u6027\u6765\u5e2e\u52a9\u6211\u4eec\u5b8c\u6210\u8fd9\u9879\u5de5\u4f5c\uff0c\u5b83\u5c31\u662f<strong>\u53f3\u503c\u5f15\u7528<\/strong>\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u53f3\u503c\u5f15\u7528<\/h2>\n\n\n\n<p>\u53f3\u503c\u5f15\u7528\uff1a\u53f3\u503c\u5f15\u7528\u662f\u6307\u5411\u53f3\u503c\u7684\u5f15\u7528\u3002\u53f3\u503c\u5f15\u7528\u4f7f\u7528\u4e24\u4e2a\u7b26\u53f7&amp;&amp;\u8868\u793a\uff0c\u5373&amp;&amp;\u3002\u53f3\u503c\u5f15\u7528\u7684\u4e3b\u8981\u4f5c\u7528\u662f\u652f\u6301<strong>\u79fb\u52a8\u8bed\u4e49<\/strong>\u548c<strong>\u5b8c\u7f8e\u8f6c\u53d1<\/strong>\u3002\u901a\u8fc7\u79fb\u52a8\u8bed\u4e49\uff0c\u6211\u4eec\u53ef\u4ee5\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u64cd\u4f5c\uff0c\u63d0\u9ad8\u6027\u80fd\u3002<\/p>\n\n\n\n<p>\u5982\u4e0b\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#f6f6f4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#282A36\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#f6f6f4;display:none\" aria-label=\"\u590d\u5236\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>int main() {\n    int i = 0;\n    int &amp;j = i; \/* lvalue ref *\/\n    int &amp;&amp;k = 10; \/* rvalue ref *\/\n}<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki dracula-soft\" style=\"background-color: #282A36\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F286C4\">int<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #62E884\">main<\/span><span style=\"color: #F6F6F4\">() {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    <\/span><span style=\"color: #F286C4\">int<\/span><span style=\"color: #F6F6F4\"> i <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">0<\/span><span style=\"color: #F6F6F4\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    <\/span><span style=\"color: #F286C4\">int<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #F286C4\">&amp;<\/span><span style=\"color: #F6F6F4\">j <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> i;<\/span><span style=\"color: #7B7F8B\"> \/* lvalue ref *\/<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    <\/span><span style=\"color: #F286C4\">int<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #F286C4\">&amp;&amp;<\/span><span style=\"color: #F6F6F4\">k <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">10<\/span><span style=\"color: #F6F6F4\">;<\/span><span style=\"color: #7B7F8B\"> \/* rvalue ref *\/<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">}<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>\u8fd9\u91cc\u7684k\u5c31\u662f\u4e00\u4e2a\u53f3\u503c\u5f15\u7528\uff0c\u5982\u679ck\u53bb\u5f15\u7528i\u7684\u8bdd\u4f1a\u5f15\u8d77\u7f16\u8bd1\u9519\u8bef\u3002<\/p>\n\n\n\n<p>\u53f3\u503c\u5f15\u7528\u7684\u7279\u70b9\u662f\u53ef\u4ee5\u5ef6\u957f\u53f3\u503c\u7684\u751f\u547d\u5468\u671f\uff0c\u770b\u5982\u4e0b\u793a\u4f8b<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#f6f6f4;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#282A36\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#f6f6f4;display:none\" aria-label=\"\u590d\u5236\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>#include &lt;iostream>\n\nclass X {\npublic:\n    X() { std::cout &lt;&lt; \"X ctor\" &lt;&lt; std::endl; }\n    X(const X&amp;x) { std::cout &lt;&lt; \"X copy ctor\" &lt;&lt; std::endl; }\n    ~X() { std::cout &lt;&lt; \"X dtor\" &lt;&lt; std::endl; }\n    void show() { std::cout &lt;&lt; \"show X\" &lt;&lt; std::endl; }\n};\n\nX make_x()\n{\n    X x1;\n    return x1;\n}\n\nint main()\n{\n    int i = 0;\n    int &amp;j = i; \/* lvalue ref *\/\n    int &amp;&amp;k = 10; \/* rvalue ref *\/\n    X &amp;&amp;x2 = make_x();\n    x2.show();\n}<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki dracula-soft\" style=\"background-color: #282A36\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F286C4\">#include<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #DEE492\">&lt;<\/span><span style=\"color: #E7EE98\">iostream<\/span><span style=\"color: #DEE492\">&gt;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F286C4\">class<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #97E1F1\">X<\/span><span style=\"color: #F6F6F4\"> {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F286C4\">public:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    <\/span><span style=\"color: #62E884\">X<\/span><span style=\"color: #F6F6F4\">() { std<\/span><span style=\"color: #F286C4\">::<\/span><span style=\"color: #F6F6F4\">cout <\/span><span style=\"color: #F286C4\">&lt;&lt;<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #DEE492\">&quot;<\/span><span style=\"color: #E7EE98\">X ctor<\/span><span style=\"color: #DEE492\">&quot;<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #F286C4\">&lt;&lt;<\/span><span style=\"color: #F6F6F4\"> std<\/span><span style=\"color: #F286C4\">::<\/span><span style=\"color: #F6F6F4\">endl; }<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    <\/span><span style=\"color: #62E884\">X<\/span><span style=\"color: #F6F6F4\">(<\/span><span style=\"color: #F286C4\">const<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #97E1F1; font-style: italic\">X<\/span><span style=\"color: #F286C4\">&amp;<\/span><span style=\"color: #FFB86C; font-style: italic\">x<\/span><span style=\"color: #F6F6F4\">) { std<\/span><span style=\"color: #F286C4\">::<\/span><span style=\"color: #F6F6F4\">cout <\/span><span style=\"color: #F286C4\">&lt;&lt;<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #DEE492\">&quot;<\/span><span style=\"color: #E7EE98\">X copy ctor<\/span><span style=\"color: #DEE492\">&quot;<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #F286C4\">&lt;&lt;<\/span><span style=\"color: #F6F6F4\"> std<\/span><span style=\"color: #F286C4\">::<\/span><span style=\"color: #F6F6F4\">endl; }<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    <\/span><span style=\"color: #62E884\">~X<\/span><span style=\"color: #F6F6F4\">() { std<\/span><span style=\"color: #F286C4\">::<\/span><span style=\"color: #F6F6F4\">cout <\/span><span style=\"color: #F286C4\">&lt;&lt;<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #DEE492\">&quot;<\/span><span style=\"color: #E7EE98\">X dtor<\/span><span style=\"color: #DEE492\">&quot;<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #F286C4\">&lt;&lt;<\/span><span style=\"color: #F6F6F4\"> std<\/span><span style=\"color: #F286C4\">::<\/span><span style=\"color: #F6F6F4\">endl; }<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    <\/span><span style=\"color: #F286C4\">void<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #62E884\">show<\/span><span style=\"color: #F6F6F4\">() { std<\/span><span style=\"color: #F286C4\">::<\/span><span style=\"color: #F6F6F4\">cout <\/span><span style=\"color: #F286C4\">&lt;&lt;<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #DEE492\">&quot;<\/span><span style=\"color: #E7EE98\">show X<\/span><span style=\"color: #DEE492\">&quot;<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #F286C4\">&lt;&lt;<\/span><span style=\"color: #F6F6F4\"> std<\/span><span style=\"color: #F286C4\">::<\/span><span style=\"color: #F6F6F4\">endl; }<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">};<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #97E1F1; font-style: italic\">X<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #62E884\">make_x<\/span><span style=\"color: #F6F6F4\">()<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    X x1;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    <\/span><span style=\"color: #F286C4\">return<\/span><span style=\"color: #F6F6F4\"> x1;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">}<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F286C4\">int<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #62E884\">main<\/span><span style=\"color: #F6F6F4\">()<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    <\/span><span style=\"color: #F286C4\">int<\/span><span style=\"color: #F6F6F4\"> i <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">0<\/span><span style=\"color: #F6F6F4\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    <\/span><span style=\"color: #F286C4\">int<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #F286C4\">&amp;<\/span><span style=\"color: #F6F6F4\">j <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> i;<\/span><span style=\"color: #7B7F8B\"> \/* lvalue ref *\/<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    <\/span><span style=\"color: #F286C4\">int<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #F286C4\">&amp;&amp;<\/span><span style=\"color: #F6F6F4\">k <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">10<\/span><span style=\"color: #F6F6F4\">;<\/span><span style=\"color: #7B7F8B\"> \/* rvalue ref *\/<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    X <\/span><span style=\"color: #F286C4\">&amp;&amp;<\/span><span style=\"color: #F6F6F4\">x2 <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #62E884\">make_x<\/span><span style=\"color: #F6F6F4\">();<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    x2.<\/span><span style=\"color: #62E884\">show<\/span><span style=\"color: #F6F6F4\">();<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">}<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>\u7f16\u8bd1\uff1a<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>g++ rvalue_ref.cpp -g -fno-elide-constructors<\/p>\n<\/blockquote>\n\n\n\n<ul class=\"wp-block-list\">\n<li>-fno-elide-constructors: <strong>\u5173\u95ed\u51fd\u6570\u8fd4\u56de\u503c\u4f18\u5316\uff08RVO\uff09<\/strong>\uff0crvo\u4f18\u5316\u4f1a\u51cf\u5c11\u590d\u5236\u6784\u9020\u51fd\u6570\u7684\u8c03\u7528\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u6d4b\u8bd5\u7ed3\u679c\uff1a<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>X ctor<\/p>\n\n\n\n<p>X copy ctor<\/p>\n\n\n\n<p>X dtor<\/p>\n\n\n\n<p>show X<\/p>\n\n\n\n<p>X dtor<\/p>\n<\/blockquote>\n\n\n\n<p>\u5982\u679c\u5c06X &amp;&amp;x2 = make_x()\u8fd9\u53e5\u4ee3\u7801\u66ff\u6362\u4e3aX x2 = make_x()\u4f1a\u53d1\u751f\u51e0\u6b21\u6784\u9020\u3002\u5728 \u6ca1\u6709\u8fdb\u884c\u4efb\u4f55\u4f18\u5316\u7684\u60c5\u51b5\u4e0b\u5e94\u8be5\u662f3\u6b21\u6784\u9020\uff0c\u9996\u5148make_x\u51fd\u6570\u4e2dx1\u4f1a \u9ed8\u8ba4\u6784\u9020\u4e00\u6b21\uff0c\u7136\u540ereturn x1\u4f1a\u4f7f\u7528\u590d\u5236\u6784\u9020\u4ea7\u751f\u4e34\u65f6\u5bf9\u8c61\uff0c\u63a5\u7740 X x2 = make_x()\u4f1a\u4f7f\u7528\u590d\u5236\u6784\u9020\u5c06\u4e34\u65f6\u5bf9\u8c61\u590d\u5236\u5230x2\uff0c\u6700\u540e\u4e34\u65f6\u5bf9\u8c61\u88ab\u9500\u6bc1\u3002<\/p>\n\n\n\n<p>\u800c\u5982\u679c\u4f7f\u7528\u4e86\u53f3\u503c\u5f15\u7528\uff0c\u5219\u6700\u540e\u201cmake_x()\u4f1a\u4f7f\u7528\u590d\u5236\u6784\u9020\u201d \u8fd9\u4e00\u6b65\u53ef\u4ee5\u7701\u7565\uff0c\u53ea\u4f1a\u53d1\u751f\u4e24\u6b21\u6784\u9020\u3002\u53ef\u4ee5\u662f\u51cf\u5c11\u5bf9\u8c61\u590d\u5236\uff0c\u63d0\u5347\u7a0b\u5e8f\u6027\u80fd\u3002<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">\u53f3\u503c\u5f15\u7528\u7684\u4f18\u5316\u4ee5\u53ca\u79fb\u52a8\u8bed\u4e49<\/h1>\n\n\n\n<p>\u6211\u4eec\u77e5\u9053\u53f3\u503c\u5728\u5f88\u591a\u60c5\u51b5\u4e0b\u90fd\u662f\u4e00\u4e2a\u4e34\u65f6\u5bf9\u8c61\uff0c\u5373\u201c\u5c06\u4ea1\u503c\u201d\u3002\u5f53\u53f3\u503c\u88ab\u4f7f\u7528\u8fc7\u540e\u7a0b\u5e8f\u4f1a\u9a6c\u4e0a\u9500\u6bc1\u5bf9\u8c61\u5e76\u91ca\u653e\u5185\u5b58\uff0c\u8fd9\u53ef\u80fd\u7684\u5bfc\u81f4\u6027\u80fd\u95ee\u9898\uff08\u5373\u9891\u7e41\u5730\u5f00\u8f9f\u548c\u91ca\u653e\u5185\u5b58\uff09\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u53f3\u503c\u5f15\u7528\u7684\u6027\u80fd\u4f18\u5316\u7a7a\u95f4<\/h2>\n\n\n\n<p>\u8ba9\u6211\u4eec\u6765\u770b\u4e0b\u9762\u7684\u4ee3\u7801\uff1a<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#f6f6f4;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#282A36\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#f6f6f4;display:none\" aria-label=\"\u590d\u5236\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>#include &lt;iostream>\n#include &lt;cstring>\n\nclass BigMemoryPool\n{\npublic:\n    static const int PoolSize = 4096;\n    BigMemoryPool() : pool_(new char&#91;PoolSize&#93;) {}\n    ~BigMemoryPool()\n    {\n        if (pool_ != nullptr)\n        {\n            delete[] pool_;\n        }\n    }\n\n    BigMemoryPool(const BigMemoryPool &amp;other) : pool_(new char&#91;PoolSize&#93;)\n    {\n        static int cnt = 1;\n        std::cout &lt;&lt; \"copy big memory pool. cnt is \" &lt;&lt; cnt++ &lt;&lt; std::endl;\n        memcpy(pool_, other.pool_, PoolSize);\n    }\n\nprivate:\n    char *pool_;\n};\n\nBigMemoryPool get_pool(const BigMemoryPool &amp;pool)\n{\n    return pool;\n}\n\nBigMemoryPool make_pool()\n{\n    BigMemoryPool pool;\n    return get_pool(pool);\n}\n\nint main()\n{\n    BigMemoryPool pool = make_pool();\n}<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki dracula-soft\" style=\"background-color: #282A36\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F286C4\">#include<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #DEE492\">&lt;<\/span><span style=\"color: #E7EE98\">iostream<\/span><span style=\"color: #DEE492\">&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F286C4\">#include<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #DEE492\">&lt;<\/span><span style=\"color: #E7EE98\">cstring<\/span><span style=\"color: #DEE492\">&gt;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F286C4\">class<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #97E1F1\">BigMemoryPool<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F286C4\">public:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    <\/span><span style=\"color: #F286C4\">static<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #F286C4\">const<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #F286C4\">int<\/span><span style=\"color: #F6F6F4\"> PoolSize <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">4096<\/span><span style=\"color: #F6F6F4\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    <\/span><span style=\"color: #62E884\">BigMemoryPool<\/span><span style=\"color: #F6F6F4\">() : <\/span><span style=\"color: #62E884\">pool_<\/span><span style=\"color: #F6F6F4\">(<\/span><span style=\"color: #F286C4; font-weight: bold\">new<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #F286C4\">char<\/span><span style=\"color: #F6F6F4\">&#91;PoolSize&#93;) {}<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    <\/span><span style=\"color: #62E884\">~BigMemoryPool<\/span><span style=\"color: #F6F6F4\">()<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">        <\/span><span style=\"color: #F286C4\">if<\/span><span style=\"color: #F6F6F4\"> (pool_ <\/span><span style=\"color: #F286C4\">!=<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">nullptr<\/span><span style=\"color: #F6F6F4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">        {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">            <\/span><span style=\"color: #F286C4\">delete[]<\/span><span style=\"color: #F6F6F4\"> pool_;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">        }<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    }<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    <\/span><span style=\"color: #62E884\">BigMemoryPool<\/span><span style=\"color: #F6F6F4\">(<\/span><span style=\"color: #F286C4\">const<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #97E1F1; font-style: italic\">BigMemoryPool<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #F286C4\">&amp;<\/span><span style=\"color: #FFB86C; font-style: italic\">other<\/span><span style=\"color: #F6F6F4\">) : <\/span><span style=\"color: #62E884\">pool_<\/span><span style=\"color: #F6F6F4\">(<\/span><span style=\"color: #F286C4; font-weight: bold\">new<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #F286C4\">char<\/span><span style=\"color: #F6F6F4\">&#91;PoolSize&#93;)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">        <\/span><span style=\"color: #F286C4\">static<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #F286C4\">int<\/span><span style=\"color: #F6F6F4\"> cnt <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">1<\/span><span style=\"color: #F6F6F4\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">        std<\/span><span style=\"color: #F286C4\">::<\/span><span style=\"color: #F6F6F4\">cout <\/span><span style=\"color: #F286C4\">&lt;&lt;<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #DEE492\">&quot;<\/span><span style=\"color: #E7EE98\">copy big memory pool. cnt is <\/span><span style=\"color: #DEE492\">&quot;<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #F286C4\">&lt;&lt;<\/span><span style=\"color: #F6F6F4\"> cnt<\/span><span style=\"color: #F286C4\">++<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #F286C4\">&lt;&lt;<\/span><span style=\"color: #F6F6F4\"> std<\/span><span style=\"color: #F286C4\">::<\/span><span style=\"color: #F6F6F4\">endl;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">        <\/span><span style=\"color: #62E884\">memcpy<\/span><span style=\"color: #F6F6F4\">(pool_, other.pool_, PoolSize);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    }<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F286C4\">private:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    <\/span><span style=\"color: #F286C4\">char<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #F286C4\">*<\/span><span style=\"color: #F6F6F4\">pool_;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">};<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #97E1F1; font-style: italic\">BigMemoryPool<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #62E884\">get_pool<\/span><span style=\"color: #F6F6F4\">(<\/span><span style=\"color: #F286C4\">const<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #97E1F1; font-style: italic\">BigMemoryPool<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #F286C4\">&amp;<\/span><span style=\"color: #FFB86C; font-style: italic\">pool<\/span><span style=\"color: #F6F6F4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    <\/span><span style=\"color: #F286C4\">return<\/span><span style=\"color: #F6F6F4\"> pool;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">}<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #97E1F1; font-style: italic\">BigMemoryPool<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #62E884\">make_pool<\/span><span style=\"color: #F6F6F4\">()<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    BigMemoryPool pool;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    <\/span><span style=\"color: #F286C4\">return<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #62E884\">get_pool<\/span><span style=\"color: #F6F6F4\">(pool);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">}<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F286C4\">int<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #62E884\">main<\/span><span style=\"color: #F6F6F4\">()<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    BigMemoryPool pool <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #62E884\">make_pool<\/span><span style=\"color: #F6F6F4\">();<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">}<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>\u4ee5\u4e0a\u4ee3\u7801\u7684\u4e3b\u8981\u4f5c\u7528\u662f\u6784\u9020\u5185\u5b58\u6c60\uff0c\u90a3\u4e48\u5728\u4f7f\u7528C11\u6807\u51c6\u4e14\u4e0d\u5f00\u542fROV\u7684\u60c5\u51b5\u4e0b\uff0c\u4e0a\u8ff0\u4ee3\u7801\u8fdb\u884c\u4e86\u51e0\u6b21copy\uff1f<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u5173\u952e\u6b65\u9aa4\u5206\u6790<\/strong><\/h3>\n\n\n\n<ol start=\"1\" class=\"wp-block-list\">\n<li><code>make_pool()<\/code><strong> \u51fd\u6570\u5185<\/strong>\uff1a\n<ol class=\"wp-block-list\">\n<li>\u521b\u5efa\u5c40\u90e8\u5bf9\u8c61 <code>pool<\/code>\uff08\u8c03\u7528\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\uff0c\u65e0\u590d\u5236\uff09\u3002<\/li>\n\n\n\n<li>\u8c03\u7528 <code>get_pool(pool)<\/code>\uff0c\u53c2\u6570\u4e3a <code>const BigMemoryPool&amp;<\/code>\uff08\u5f15\u7528\u4f20\u9012\uff0c\u65e0\u590d\u5236\uff09\u3002<\/li>\n<\/ol>\n<\/li>\n\n\n\n<li><code>get_pool()<\/code><strong> \u51fd\u6570\u5185<\/strong>\uff1a\n<ol class=\"wp-block-list\">\n<li>\u8fd4\u56de <code>pool<\/code> \u65f6\uff0c\u7531\u4e8e\u51fd\u6570\u8fd4\u56de\u7c7b\u578b\u662f <code>BigMemoryPool<\/code>\uff08\u503c\u8fd4\u56de\uff09\uff0c\u9700\u8981\u4ece\u5f15\u7528\u751f\u6210\u4e34\u65f6\u5bf9\u8c61\u3002<strong>\u6b64\u5904\u89e6\u53d1 1 \u6b21\u590d\u5236\u6784\u9020\u51fd\u6570<\/strong>\uff08\u8f93\u51fa <code>cnt=1<\/code>\uff09\u3002<\/li>\n<\/ol>\n<\/li>\n\n\n\n<li><code>make_pool()<\/code><strong> \u7684\u8fd4\u56de\u503c<\/strong>\uff1a\n<ol class=\"wp-block-list\">\n<li><code>return get_pool(pool)<\/code> \u65f6\uff0c\u82e5\u7f16\u8bd1\u5668\u542f\u7528\u8fd4\u56de\u503c\u4f18\u5316\uff08RVO\uff09\uff0c\u4f1a\u76f4\u63a5\u5c06 <code>get_pool<\/code> \u7684\u8fd4\u56de\u503c\u6784\u9020\u5230 <code>make_pool<\/code> \u7684\u8fd4\u56de\u4f4d\u7f6e\uff0c<strong>\u907f\u514d\u989d\u5916\u590d\u5236<\/strong>\u3002\u82e5\u672a\u5f00\u542fROV\uff0c\u5219<strong>\u6b64\u5904\u89e6\u53d1 1 \u6b21\u590d\u5236\u6784\u9020\u51fd\u6570<\/strong>\uff08\u8f93\u51fa <code>cnt=2<\/code>\uff09\u3002<\/li>\n<\/ol>\n<\/li>\n\n\n\n<li><code>main()<\/code><strong> \u4e2d\u521d\u59cb\u5316 <\/strong><code>pool<\/code>\uff1a\n<ol class=\"wp-block-list\">\n<li><code>BigMemoryPool pool = make_pool();<\/code> \u89e6\u53d1 C++17 \u7684\u5f3a\u5236\u590d\u5236\u7701\u7565\uff08Mandatory Copy Elision\uff09\uff0c<strong>\u65e0\u989d\u5916\u590d\u5236<\/strong>\u3002\u82e5\u4f7f\u7528C11\u6807\u51c6\uff0c<strong>\u6b64\u5904\u89e6\u53d1 1 \u6b21\u590d\u5236\u6784\u9020\u51fd\u6570<\/strong>\uff08\u8f93\u51fa <code>cnt=3<\/code>\uff09\u3002<\/li>\n<\/ol>\n<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">\u6d4b\u8bd5\u7ed3\u679c<\/h3>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u7f16\u8bd1\uff1ag++ MempoolMultiCopy.cpp -g -fno-elide-constructors -std=c++11<\/p>\n\n\n\n<p>copy big memory pool. cnt is 1<\/p>\n\n\n\n<p>copy big memory pool. cnt is 2<\/p>\n\n\n\n<p>copy big memory pool. cnt is 3<\/p>\n<\/blockquote>\n\n\n\n<h1 class=\"wp-block-heading\">\u79fb\u52a8\u8bed\u4e49\u548c\u5b8c\u7f8e\u8f6c\u53d1<\/h1>\n\n\n\n<p>\u5982\u679c\u6211\u4eec\u80fd\u591f\u6d88\u9664\u590d\u5236\u4e34\u65f6\u5bf9\u8c61\u5e26\u6765\u7684\u6027\u80fd\u5f00\u9500\uff0c\u5c82\u4e0d\u7f8e\u54c9\uff1f\u8fd9\u5c31\u662f<strong>\u79fb\u52a8\u8bed\u4e49<\/strong>\u3002<\/p>\n\n\n\n<p>\u8ba9\u6211\u4eec\u4fee\u6539<code>BigMemoryPool<\/code>\u7c7b\uff1a<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#f6f6f4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#282A36\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#f6f6f4;display:none\" aria-label=\"\u590d\u5236\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>    BigMemoryPool(BigMemoryPool &amp;&amp;other)\n    {\n        std::cout &lt;&lt; \"move big memory pool\" &lt;&lt; std::endl;\n        pool_ = other.pool_;\n        other.pool_ = nullptr;\n    }<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki dracula-soft\" style=\"background-color: #282A36\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F6F6F4\">    <\/span><span style=\"color: #62E884\">BigMemoryPool<\/span><span style=\"color: #F6F6F4\">(BigMemoryPool <\/span><span style=\"color: #F286C4\">&amp;&amp;<\/span><span style=\"color: #F6F6F4\">other)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">        std<\/span><span style=\"color: #F286C4\">::<\/span><span style=\"color: #F6F6F4\">cout <\/span><span style=\"color: #F286C4\">&lt;&lt;<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #DEE492\">&quot;<\/span><span style=\"color: #E7EE98\">move big memory pool<\/span><span style=\"color: #DEE492\">&quot;<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #F286C4\">&lt;&lt;<\/span><span style=\"color: #F6F6F4\"> std<\/span><span style=\"color: #F286C4\">::<\/span><span style=\"color: #F6F6F4\">endl;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">        pool_ <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> other.pool_;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">        other.pool_ <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">nullptr<\/span><span style=\"color: #F6F6F4\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    }<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>\u6211\u4eec\u65b0\u589e\u4e86\u4e00\u4e2a\u6784\u9020\u51fd\u6570\uff0c\u5b83\u7684\u5f62\u53c2\u662f\u4e00\u4e2a\u53f3\u503c\u5f15\u7528\u7c7b\u578b\uff0c\u79f0\u4e3a<strong>\u79fb\u52a8\u6784\u9020\u51fd\u6570<\/strong>\u3002<\/p>\n\n\n\n<p>\u7b80\u5355\u6765\u8bf4\uff0c\u79fb\u52a8\u8bed\u4e49\u662f<strong>\u6307\u9488\u7684\u66ff\u6362<\/strong>\u3002<\/p>\n\n\n\n<p>\u7f16\u8bd1\u53ca\u8f93\u51fa\u8fd9\u6bb5\u4ee3\u7801\uff0c\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='http:\/\/yangnanbei.top\/wp-content\/uploads\/2025\/09\/image-1024x106.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"106\" data-original=\"http:\/\/yangnanbei.top\/wp-content\/uploads\/2025\/09\/image-1024x106.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" class=\"wp-image-460\"  sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/div><\/figure>\n\n\n\n<p>\u53ef\u4ee5\u770b\u5230\u540e\u4e24\u6b21\u7684\u6784\u9020\u51fd\u6570\u53d8\u6210\u4e86\u79fb\u52a8\u6784\u9020\u51fd\u6570\uff0c\u56e0\u4e3a\u8fd9\u4e24\u6b21\u64cd\u4f5c\u4e2d\u6e90\u5bf9\u8c61\u90fd\u662f\u53f3\u503c\uff0c\u5bf9\u4e8e\u53f3\u503c\u7f16\u8bd1\u5668\u4f1a\u4f18\u5148\u9009\u7528\u79fb\u52a8\u6784\u9020\u51fd\u6570\u53bb\u6784\u9020\u5bf9\u8c61\u3002<\/p>\n\n\n\n<p>\u9664\u4e86\u79fb\u52a8\u6784\u9020\u51fd\u6570\u5916\uff0c\u79fb\u52a8\u590d\u5236\u8fd0\u7b97\u7b26\u4e5f\u80fd\u5b8c\u6210\u79fb\u52a8\u7684\u64cd\u4f5c\u3002<\/p>\n\n\n\n<p>\u7ee7\u7eed\u5728\u7c7b\u4e2d\u65b0\u589e\u79fb\u52a8\u8d4b\u503c\u8fd0\u7b97\u7b26\u51fd\u6570\uff1a<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#f6f6f4;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#282A36\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#f6f6f4;display:none\" aria-label=\"\u590d\u5236\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>    BigMemoryPool &amp;operator=(BigMemoryPool &amp;&amp;other)\n    {\n        std::cout &lt;&lt; \"move operator(=) big memory pool.\" &lt;&lt; std::endl;\n        if (this != &amp;other)\n        {\n            if (pool_ != nullptr)\n            {\n                delete[] pool_;\n            }\n            pool_ = other.pool_;\n            other.pool_ = nullptr;\n        }\n        return *this;\n    }\n    ...\n    \nint main()\n{\n    BigMemoryPool my_pool;\n    my_pool = make_pool();\n}<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki dracula-soft\" style=\"background-color: #282A36\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F6F6F4\">    <\/span><span style=\"color: #97E1F1; font-style: italic\">BigMemoryPool<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #F286C4\">&amp;operator<\/span><span style=\"color: #F6F6F4\">=(<\/span><span style=\"color: #97E1F1; font-style: italic\">BigMemoryPool<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #F286C4\">&amp;&amp;<\/span><span style=\"color: #FFB86C; font-style: italic\">other<\/span><span style=\"color: #F6F6F4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">        std<\/span><span style=\"color: #F286C4\">::<\/span><span style=\"color: #F6F6F4\">cout <\/span><span style=\"color: #F286C4\">&lt;&lt;<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #DEE492\">&quot;<\/span><span style=\"color: #E7EE98\">move operator(=) big memory pool.<\/span><span style=\"color: #DEE492\">&quot;<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #F286C4\">&lt;&lt;<\/span><span style=\"color: #F6F6F4\"> std<\/span><span style=\"color: #F286C4\">::<\/span><span style=\"color: #F6F6F4\">endl;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">        <\/span><span style=\"color: #F286C4\">if<\/span><span style=\"color: #F6F6F4\"> (<\/span><span style=\"color: #BF9EEE; font-style: italic\">this<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #F286C4\">!=<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #F286C4\">&amp;<\/span><span style=\"color: #F6F6F4\">other)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">        {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">            <\/span><span style=\"color: #F286C4\">if<\/span><span style=\"color: #F6F6F4\"> (pool_ <\/span><span style=\"color: #F286C4\">!=<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">nullptr<\/span><span style=\"color: #F6F6F4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">            {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">                <\/span><span style=\"color: #F286C4\">delete[]<\/span><span style=\"color: #F6F6F4\"> pool_;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">            }<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">            pool_ <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> other.pool_;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">            other.pool_ <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">nullptr<\/span><span style=\"color: #F6F6F4\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">        }<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">        <\/span><span style=\"color: #F286C4\">return<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #F286C4\">*<\/span><span style=\"color: #BF9EEE; font-style: italic\">this<\/span><span style=\"color: #F6F6F4\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    }<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    ...<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    <\/span><\/span>\n<span class=\"line\"><span style=\"color: #F286C4\">int<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #62E884\">main<\/span><span style=\"color: #F6F6F4\">()<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    BigMemoryPool my_pool;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    my_pool <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #62E884\">make_pool<\/span><span style=\"color: #F6F6F4\">();<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">}<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>\u8fd9\u6bb5\u4ee3\u7801\u7f16\u8bd1\u8fd0\u884c\u7684\u7ed3\u679c\u662f\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='http:\/\/yangnanbei.top\/wp-content\/uploads\/2025\/09\/image-1.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  loading=\"lazy\" decoding=\"async\" width=\"393\" height=\"66\" data-original=\"http:\/\/yangnanbei.top\/wp-content\/uploads\/2025\/09\/image-1.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" class=\"wp-image-461\"  sizes=\"(max-width: 393px) 100vw, 393px\" \/><\/div><\/figure>\n\n\n\n<p>\u53ef\u4ee5\u770b\u5230\u7f16\u8bd1\u5668\u5bf9\u6e90\u5bf9\u8c61\u662f\u53f3\u503c\u7684\u60c5\u51b5\u4f1a\u4f18\u5148\u8c03\u7528\u79fb\u52a8\u8d4b\u503c\u8fd0\u7b97\u7b26\u51fd\u6570\uff0c\u5982\u679c\u8be5\u51fd\u6570\u4e0d\u5b58\u5728\uff0c\u5219\u8c03\u7528\u590d\u5236\u8d4b\u503c\u8fd0\u7b97\u7b26\u51fd\u6570\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u5c06\u5de6\u503c\u8f6c\u5316\u4e3a\u53f3\u503c<\/h3>\n\n\n\n<p>\u56e0\u4e3a\u53f3\u503c\u5f15\u7528\u53ea\u80fd\u7ed1\u5b9a\u4e00\u4e2a\u53f3\u503c\uff0c\u5982\u679c\u5c1d\u8bd5\u7ed1\u5b9a\uff0c\u5de6\u503c\u4f1a\u5bfc\u81f4\u7f16\u8bd1\u9519\u8bef\u3002<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#f6f6f4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#282A36\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#f6f6f4;display:none\" aria-label=\"\u590d\u5236\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>int i = 0;\nint &amp;&amp;k = i; \/\/compile fail<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki dracula-soft\" style=\"background-color: #282A36\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F286C4\">int<\/span><span style=\"color: #F6F6F4\"> i <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">0<\/span><span style=\"color: #F6F6F4\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F286C4\">int<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #F286C4\">&amp;&amp;<\/span><span style=\"color: #F6F6F4\">k <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> i;<\/span><span style=\"color: #7B7F8B\"> \/\/compile fail<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>\u4f46\u662f\u5728C11\u6807\u51c6\u540e\u53ef\u4ee5\u5728\u4e0d\u521b\u5efa\u4e34\u65f6\u503c\u7684\u60c5\u51b5\u4e0b\u663e\u793a\u5730\u5c06\u5de6\u503c\u901a\u8fc7&lt;static_cast&gt;\u8f6c\u5316\u4e3a\u5c06\u4ea1\u503c\u3002<\/p>\n\n\n\n<p>\u7531\u4e8e\u5c06\u4ea1\u503c\u5c5e\u4e8e\u53f3\u503c\uff0c\u6240\u4ee5\u53ef\u4ee5\u88ab\u53f3\u503c\u7ed1\u5b9a\u3002<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#f6f6f4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#282A36\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#f6f6f4;display:none\" aria-label=\"\u590d\u5236\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>int i = 0;\nint &amp;&amp;k = static_cast&lt;int&amp;&amp;> i;<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki dracula-soft\" style=\"background-color: #282A36\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F286C4\">int<\/span><span style=\"color: #F6F6F4\"> i <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">0<\/span><span style=\"color: #F6F6F4\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F286C4\">int<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #F286C4\">&amp;&amp;<\/span><span style=\"color: #F6F6F4\">k <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #F286C4\">static_cast&lt;int&amp;&amp;&gt;<\/span><span style=\"color: #F6F6F4\"> i;<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>\u8fd9\u5b58\u5728\u7684\u610f\u4e49\u662f\u4ec0\u4e48\u5462\uff1f<\/p>\n\n\n\n<p>\u6b63\u786e\u7684\u4f7f\u7528\u573a\u666f\u662f<strong>\u5728\u4e00\u4e2a\u53f3\u503c\u88ab\u8f6c\u5316\u4e3a<\/strong><strong>\u5de6\u503c<\/strong><strong>\u540e\u9700\u8981\u518d\u6b21\u8f6c\u5316\u4e3a\u53f3\u503c<\/strong>\u3002<\/p>\n\n\n\n<p>\u6700\u5178\u578b\u7684\u4f8b\u5b50\u662f\u4e00\u4e2a\u53f3\u503c\u4f5c\u4e3a\u5b9e\u53c2\u4f20\u5165\u5230\u51fd\u6570\u4e2d\uff0c\u56e0\u4e3a\u65e0\u8bba\u51fd\u6570\u7684\u5b9e\u53c2\u662f\u5de6\u503c\u8fd8\u662f\u53f3\u503c\uff0c\u51fd\u6570\u7684\u5f62\u53c2\u90fd\u662f\u5de6\u503c\u3002\u5373\u4f7f\u8fd9\u4e2a\u5f62\u53c2\u770b\u4e0a\u53bb\u662f\u4e00\u4e2a\u53f3\u503c\u5f15\u7528\uff0c\u5982\uff1a<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#f6f6f4;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#282A36\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#f6f6f4;display:none\" aria-label=\"\u590d\u5236\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>void move_pool(BigMemoryPool &amp;&amp;pool)\n{\n    std::cout &lt;&lt; \"call move_pool\" &lt;&lt; std::endl;\n    BigMemoryPool my_pool(pool);\n}\n\nint main()\n{\n    move_pool(make_pool());\n}<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki dracula-soft\" style=\"background-color: #282A36\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F286C4\">void<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #62E884\">move_pool<\/span><span style=\"color: #F6F6F4\">(<\/span><span style=\"color: #97E1F1; font-style: italic\">BigMemoryPool<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #F286C4\">&amp;&amp;<\/span><span style=\"color: #FFB86C; font-style: italic\">pool<\/span><span style=\"color: #F6F6F4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    std<\/span><span style=\"color: #F286C4\">::<\/span><span style=\"color: #F6F6F4\">cout <\/span><span style=\"color: #F286C4\">&lt;&lt;<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #DEE492\">&quot;<\/span><span style=\"color: #E7EE98\">call move_pool<\/span><span style=\"color: #DEE492\">&quot;<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #F286C4\">&lt;&lt;<\/span><span style=\"color: #F6F6F4\"> std<\/span><span style=\"color: #F286C4\">::<\/span><span style=\"color: #F6F6F4\">endl;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    BigMemoryPool <\/span><span style=\"color: #62E884\">my_pool<\/span><span style=\"color: #F6F6F4\">(pool);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">}<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F286C4\">int<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #62E884\">main<\/span><span style=\"color: #F6F6F4\">()<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    <\/span><span style=\"color: #62E884\">move_pool<\/span><span style=\"color: #F6F6F4\">(<\/span><span style=\"color: #62E884\">make_pool<\/span><span style=\"color: #F6F6F4\">());<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">}<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>\u7f16\u8bd1\u8fd0\u884c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='http:\/\/yangnanbei.top\/wp-content\/uploads\/2025\/09\/image-2.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  loading=\"lazy\" decoding=\"async\" width=\"343\" height=\"82\" data-original=\"http:\/\/yangnanbei.top\/wp-content\/uploads\/2025\/09\/image-2.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" class=\"wp-image-462\"  sizes=\"(max-width: 343px) 100vw, 343px\" \/><\/div><\/figure>\n\n\n\n<p>\u540c\u65f6\uff0c\u4e3a\u4e86\u8ba9my_pool\u8c03\u7528\u79fb\u52a8\u6784\u9020\u51fd\u6570\u8fdb\u884c\u6784\u9020\uff0c\u9700\u8981\u5c06\u5f62\u53c2pool\u8f6c\u4e3a\u53f3\u503c\u3002<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#f6f6f4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#282A36\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#f6f6f4;display:none\" aria-label=\"\u590d\u5236\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>void move_pool(BigMemoryPool &amp;&amp;pool)\n{\n    std::cout &lt;&lt; \"call move_pool\" &lt;&lt; std::endl;\n    BigMemoryPool my_pool(static_cast&lt;BigMemoryPool&amp;&amp;>(pool));\n}<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki dracula-soft\" style=\"background-color: #282A36\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F286C4\">void<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #62E884\">move_pool<\/span><span style=\"color: #F6F6F4\">(<\/span><span style=\"color: #97E1F1; font-style: italic\">BigMemoryPool<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #F286C4\">&amp;&amp;<\/span><span style=\"color: #FFB86C; font-style: italic\">pool<\/span><span style=\"color: #F6F6F4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    std<\/span><span style=\"color: #F286C4\">::<\/span><span style=\"color: #F6F6F4\">cout <\/span><span style=\"color: #F286C4\">&lt;&lt;<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #DEE492\">&quot;<\/span><span style=\"color: #E7EE98\">call move_pool<\/span><span style=\"color: #DEE492\">&quot;<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #F286C4\">&lt;&lt;<\/span><span style=\"color: #F6F6F4\"> std<\/span><span style=\"color: #F286C4\">::<\/span><span style=\"color: #F6F6F4\">endl;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    BigMemoryPool <\/span><span style=\"color: #62E884\">my_pool<\/span><span style=\"color: #F6F6F4\">(<\/span><span style=\"color: #F286C4\">static_cast&lt;<\/span><span style=\"color: #F6F6F4\">BigMemoryPool<\/span><span style=\"color: #F286C4\">&amp;&amp;&gt;<\/span><span style=\"color: #F6F6F4\">(pool));<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">}<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>\u5728cpp\u7684\u6807\u51c6\u5e93\u4e2d\u63d0\u4f9b\u4e86\u51fd\u6570\u6a21\u7248<code>std::move<\/code>\u5e2e\u52a9\u6211\u4eec\u5c06\u5de6\u503c\u8f6c\u5316\u4e3a\u53f3\u503c\u3002\u8fd9\u4e2a\u51fd\u6570\u5185\u90e8\u4e5f\u662f\u7528static_cast\u505a\u8f6c\u5316\uff0c\u53ea\u662f\u7531\u4e8e\u5b83\u662f\u7528\u6a21\u7248\u5b9e\u73b0\u7684\uff0c\u53ef\u4ee5\u81ea\u52a8\u63a8\u5bfc\u7c7b\u578b\u3002\u6240\u4ee5\u4ee5\u540e\u4f7f\u7528<code>std::move<\/code>\u800c\u4e0d\u662f<code>static_cast<\/code>.<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#f6f6f4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#282A36\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#f6f6f4;display:none\" aria-label=\"\u590d\u5236\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>void move_pool(BigMemoryPool &amp;&amp;pool)\n{\n    std::cout &lt;&lt; \"call move_pool\" &lt;&lt; std::endl;\n    BigMemoryPool my_pool(std::move(pool));\n}<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki dracula-soft\" style=\"background-color: #282A36\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F286C4\">void<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #62E884\">move_pool<\/span><span style=\"color: #F6F6F4\">(<\/span><span style=\"color: #97E1F1; font-style: italic\">BigMemoryPool<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #F286C4\">&amp;&amp;<\/span><span style=\"color: #FFB86C; font-style: italic\">pool<\/span><span style=\"color: #F6F6F4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    std<\/span><span style=\"color: #F286C4\">::<\/span><span style=\"color: #F6F6F4\">cout <\/span><span style=\"color: #F286C4\">&lt;&lt;<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #DEE492\">&quot;<\/span><span style=\"color: #E7EE98\">call move_pool<\/span><span style=\"color: #DEE492\">&quot;<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #F286C4\">&lt;&lt;<\/span><span style=\"color: #F6F6F4\"> std<\/span><span style=\"color: #F286C4\">::<\/span><span style=\"color: #F6F6F4\">endl;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    BigMemoryPool <\/span><span style=\"color: #62E884\">my_pool<\/span><span style=\"color: #F6F6F4\">(std<\/span><span style=\"color: #F286C4\">::<\/span><span style=\"color: #62E884\">move<\/span><span style=\"color: #F6F6F4\">(pool));<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">}<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">\u4e07\u80fd\u5f15\u7528\u548c\u6298\u53e0\u5f15\u7528<\/h3>\n\n\n\n<p>\u6211\u4eec\u4e4b\u524d\u63d0\u5230\u8fc7\u5e38\u91cf\u5de6\u503c\u5f15\u7528\u65e2\u53ef\u4ee5\u5f15\u7528\u5de6\u503c\u53c8\u53ef\u4ee5\u5f15\u7528\u53f3\u503c\uff0c\u662f\u4e00\u4e2a\u51e0\u4e4e\u4e07\u80fd\u7684\u5f15\u7528\uff0c\u4f46\u662f\u53ef\u60dc\u7684\u662f\u7531\u4e8e\u5176\u5e38\u91cf\u5c5e\u6027\uff0c\u5bfc\u81f4\u4f7f\u7528\u8303\u56f4\u53d7\u9650\u3002\u5176\u5b9e\u5728C++11\u6807\u51c6\u4e2d\u63d0\u4f9b\u4e86\u4e00\u4e2a&#8221;\u4e07\u80fd&#8221;\u5f15\u7528\u3002<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#f6f6f4;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#282A36\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#f6f6f4;display:none\" aria-label=\"\u590d\u5236\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>void foo(int &amp;&amp;i) {} \/\/i\u662f\u53f3\u503c\u5f15\u7528\n\ntemplate&lt;class T>\nvoid bar(T &amp;&amp;t) {} \/\/t\u662f\u4e07\u80fd\u5f15\u7528\n\nint get_val()\n{\n    return 1;\n}\n\nint main()\n{\n    int &amp;&amp;x = get_val(); \/\/\u53f3\u503c\u5f15\u7528\n    auto &amp;&amp;y = get_val(); \/\/\u4e07\u80fd\u5f15\u7528\n}<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki dracula-soft\" style=\"background-color: #282A36\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F286C4\">void<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #62E884\">foo<\/span><span style=\"color: #F6F6F4\">(<\/span><span style=\"color: #F286C4\">int<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #F286C4\">&amp;&amp;<\/span><span style=\"color: #FFB86C; font-style: italic\">i<\/span><span style=\"color: #F6F6F4\">) {}<\/span><span style=\"color: #7B7F8B\"> \/\/i\u662f\u53f3\u503c\u5f15\u7528<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F286C4\">template<\/span><span style=\"color: #F6F6F4\">&lt;<\/span><span style=\"color: #F286C4\">class<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #97E1F1; font-style: italic\">T<\/span><span style=\"color: #F6F6F4\">&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F286C4\">void<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #62E884\">bar<\/span><span style=\"color: #F6F6F4\">(<\/span><span style=\"color: #97E1F1; font-style: italic\">T<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #F286C4\">&amp;&amp;<\/span><span style=\"color: #FFB86C; font-style: italic\">t<\/span><span style=\"color: #F6F6F4\">) {}<\/span><span style=\"color: #7B7F8B\"> \/\/t\u662f\u4e07\u80fd\u5f15\u7528<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F286C4\">int<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #62E884\">get_val<\/span><span style=\"color: #F6F6F4\">()<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    <\/span><span style=\"color: #F286C4\">return<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">1<\/span><span style=\"color: #F6F6F4\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">}<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F286C4\">int<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #62E884\">main<\/span><span style=\"color: #F6F6F4\">()<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    <\/span><span style=\"color: #F286C4\">int<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #F286C4\">&amp;&amp;<\/span><span style=\"color: #F6F6F4\">x <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #62E884\">get_val<\/span><span style=\"color: #F6F6F4\">();<\/span><span style=\"color: #7B7F8B\"> \/\/\u53f3\u503c\u5f15\u7528<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    <\/span><span style=\"color: #F286C4\">auto<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #F286C4\">&amp;&amp;<\/span><span style=\"color: #F6F6F4\">y <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #62E884\">get_val<\/span><span style=\"color: #F6F6F4\">();<\/span><span style=\"color: #7B7F8B\"> \/\/\u4e07\u80fd\u5f15\u7528<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">}<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p><strong>\u4e07\u80fd\u5f15\u7528<\/strong>\u5b9e\u9645\u4e0a\u662f\u53d1\u751f\u4e86\u7c7b\u578b\u63a8\u5bfc\uff0c\u5982\u679c\u6e90\u5bf9\u8c61\u662f\u5de6\u503c\uff0c\u5219\u4f1a\u63a8\u5bfc\u51fa\u5de6\u503c\u5f15\u7528\uff0c\u5982\u679c\u6e90\u5bf9\u8c61\u662f\u53f3\u503c\uff0c\u5219\u4f1a\u63a8\u5bfc\u51fa\u53f3\u503c\u5f15\u7528\u3002<\/p>\n\n\n\n<p>\u4e07\u80fd\u5f15\u7528\u80fd\u591f\u7075\u6d3b\u5730\u5e94\u7528\u5bf9\u8c61\uff0c\u662f\u56e0\u4e3a\u6709\u5f15\u7528\u6298\u53e0\u3002<\/p>\n\n\n\n<p>\u4e07\u80fd\u5f15\u7528\u7684\u5f62\u5f0f\u5fc5\u987b\u662f<code>T&amp;&amp;<\/code>\u6216\u8005<code>auto&amp;&amp;<\/code>\uff0c\u4e5f\u5c31\u662f\u8bf4\u5b83\u4eec\u5fc5\u987b\u5728\u521d\u59cb\u5316\u7684\u65f6\u5019\u88ab\u76f4\u63a5\u63a8\u5bfc\u51fa\u6765\uff0c\u5982\u679c\u5728\u63a8\u5bfc\u4e2d\u51fa\u73b0\u4e2d\u95f4\u8fc7\u7a0b\uff0c\u5219\u4e0d\u662f\u4e00\u4e2a\u4e07\u80fd\u5f15\u7528\u3002\u5982\uff1a<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#f6f6f4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#282A36\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#f6f6f4;display:none\" aria-label=\"\u590d\u5236\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>void foo(std::vector &amp;&amp;t) {}\nint main()\n{    \n    std::vector v{ 1,2,3 };\n    foo(v);                                \/\/ \u7f16\u8bd1\u9519\u8bef  \n}<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki dracula-soft\" style=\"background-color: #282A36\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F286C4\">void<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #62E884\">foo<\/span><span style=\"color: #F6F6F4\">(std<\/span><span style=\"color: #F286C4\">::<\/span><span style=\"color: #97E1F1; font-style: italic\">vector<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #F286C4\">&amp;&amp;<\/span><span style=\"color: #FFB86C; font-style: italic\">t<\/span><span style=\"color: #F6F6F4\">) {}<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F286C4\">int<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #62E884\">main<\/span><span style=\"color: #F6F6F4\">()<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">{    <\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    std<\/span><span style=\"color: #F286C4\">::<\/span><span style=\"color: #F6F6F4\">vector v{ <\/span><span style=\"color: #BF9EEE\">1<\/span><span style=\"color: #F6F6F4\">,<\/span><span style=\"color: #BF9EEE\">2<\/span><span style=\"color: #F6F6F4\">,<\/span><span style=\"color: #BF9EEE\">3<\/span><span style=\"color: #F6F6F4\"> };<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    <\/span><span style=\"color: #62E884\">foo<\/span><span style=\"color: #F6F6F4\">(v);<\/span><span style=\"color: #7B7F8B\">                                \/\/ \u7f16\u8bd1\u9519\u8bef  <\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">}<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>\u56e0\u4e3afoo\u7684\u5f62\u53c2(t)\u5e76\u4e0d\u662f\u4e00\u4e2a\u4e07\u80fd\u5f15\u7528\uff0c\u800c\u662f\u4e00\u4e2a\u53f3\u503c\u5f15\u7528\u3002\u56e0\u4e3afoo\u7684\u5f62\u53c2\u7c7b\u578b\u662f<code>std::vector&lt;T&gt;&amp;&amp;<\/code> \u800c\u4e0d\u662f <code>T&amp;&amp;<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u5b8c\u7f8e\u8f6c\u53d1<\/h3>\n\n\n\n<p>\u4e07\u80fd\u5f15\u7528\u7684\u7528\u9014\u662f\u642d\u914d<strong>\u5b8c\u7f8e\u8f6c\u53d1<\/strong>\u3002<\/p>\n\n\n\n<p>\u5728\u4ecb\u7ecd\u5b8c\u7f8e\u8f6c\u53d1\u4e4b\u524d\uff0c\u6211\u4eec\u5148\u770b\u4e00\u4e2a\u5e38\u89c4\u7684\u8f6c\u53d1\u51fd\u6570\u6a21\u7248\u3002<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#f6f6f4;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#282A36\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#f6f6f4;display:none\" aria-label=\"\u590d\u5236\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>#include &lt;iostream>\n#include &lt;string>\n\ntemplate&lt;class T>\nvoid show_type(T t)\n{\n    std::cout &lt;&lt; typeid(t).name() &lt;&lt; std::endl;\n}\n\ntemplate&lt;class T>\nvoid normal_forwarding(T t)\n{\n    show_type(t);\n}\n\ntemplate&lt;class T>\nvoid ref_forwarding(T &amp;t)\n{\n    show_type(t);\n}\n\ntemplate&lt;class T>\nvoid const_ref_forwarding(const T &amp;t)\n{\n    show_type(t);\n}\n\ntemplate&lt;class T>\nvoid perfect_forwarding(T &amp;&amp;t)\n{\n    show_type(std::forward&lt;T>(t));\n}\n\nstd::string get_string()\n{\n    return \"hello\";\n}\n\nint main()\n{\n    std::string s = \"hello world\";\n    normal_forwarding(s);\n    perfect_forwarding(s);\n    \/\/normal_forwarding(get_string());\n}<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki dracula-soft\" style=\"background-color: #282A36\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F286C4\">#include<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #DEE492\">&lt;<\/span><span style=\"color: #E7EE98\">iostream<\/span><span style=\"color: #DEE492\">&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F286C4\">#include<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #DEE492\">&lt;<\/span><span style=\"color: #E7EE98\">string<\/span><span style=\"color: #DEE492\">&gt;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F286C4\">template<\/span><span style=\"color: #F6F6F4\">&lt;<\/span><span style=\"color: #F286C4\">class<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #97E1F1; font-style: italic\">T<\/span><span style=\"color: #F6F6F4\">&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F286C4\">void<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #62E884\">show_type<\/span><span style=\"color: #F6F6F4\">(<\/span><span style=\"color: #97E1F1; font-style: italic\">T<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #FFB86C; font-style: italic\">t<\/span><span style=\"color: #F6F6F4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    std<\/span><span style=\"color: #F286C4\">::<\/span><span style=\"color: #F6F6F4\">cout <\/span><span style=\"color: #F286C4\">&lt;&lt;<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #F286C4\">typeid<\/span><span style=\"color: #F6F6F4\">(t).<\/span><span style=\"color: #62E884\">name<\/span><span style=\"color: #F6F6F4\">() <\/span><span style=\"color: #F286C4\">&lt;&lt;<\/span><span style=\"color: #F6F6F4\"> std<\/span><span style=\"color: #F286C4\">::<\/span><span style=\"color: #F6F6F4\">endl;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">}<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F286C4\">template<\/span><span style=\"color: #F6F6F4\">&lt;<\/span><span style=\"color: #F286C4\">class<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #97E1F1; font-style: italic\">T<\/span><span style=\"color: #F6F6F4\">&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F286C4\">void<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #62E884\">normal_forwarding<\/span><span style=\"color: #F6F6F4\">(<\/span><span style=\"color: #97E1F1; font-style: italic\">T<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #FFB86C; font-style: italic\">t<\/span><span style=\"color: #F6F6F4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    <\/span><span style=\"color: #62E884\">show_type<\/span><span style=\"color: #F6F6F4\">(t);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">}<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F286C4\">template<\/span><span style=\"color: #F6F6F4\">&lt;<\/span><span style=\"color: #F286C4\">class<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #97E1F1; font-style: italic\">T<\/span><span style=\"color: #F6F6F4\">&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F286C4\">void<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #62E884\">ref_forwarding<\/span><span style=\"color: #F6F6F4\">(<\/span><span style=\"color: #97E1F1; font-style: italic\">T<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #F286C4\">&amp;<\/span><span style=\"color: #FFB86C; font-style: italic\">t<\/span><span style=\"color: #F6F6F4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    <\/span><span style=\"color: #62E884\">show_type<\/span><span style=\"color: #F6F6F4\">(t);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">}<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F286C4\">template<\/span><span style=\"color: #F6F6F4\">&lt;<\/span><span style=\"color: #F286C4\">class<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #97E1F1; font-style: italic\">T<\/span><span style=\"color: #F6F6F4\">&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F286C4\">void<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #62E884\">const_ref_forwarding<\/span><span style=\"color: #F6F6F4\">(<\/span><span style=\"color: #F286C4\">const<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #97E1F1; font-style: italic\">T<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #F286C4\">&amp;<\/span><span style=\"color: #FFB86C; font-style: italic\">t<\/span><span style=\"color: #F6F6F4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    <\/span><span style=\"color: #62E884\">show_type<\/span><span style=\"color: #F6F6F4\">(t);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">}<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F286C4\">template<\/span><span style=\"color: #F6F6F4\">&lt;<\/span><span style=\"color: #F286C4\">class<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #97E1F1; font-style: italic\">T<\/span><span style=\"color: #F6F6F4\">&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F286C4\">void<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #62E884\">perfect_forwarding<\/span><span style=\"color: #F6F6F4\">(<\/span><span style=\"color: #97E1F1; font-style: italic\">T<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #F286C4\">&amp;&amp;<\/span><span style=\"color: #FFB86C; font-style: italic\">t<\/span><span style=\"color: #F6F6F4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    <\/span><span style=\"color: #62E884\">show_type<\/span><span style=\"color: #F6F6F4\">(std<\/span><span style=\"color: #F286C4\">::<\/span><span style=\"color: #62E884\">forward<\/span><span style=\"color: #F6F6F4\">&lt;<\/span><span style=\"color: #97E1F1; font-style: italic\">T<\/span><span style=\"color: #F6F6F4\">&gt;(t));<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">}<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">std<\/span><span style=\"color: #F286C4\">::<\/span><span style=\"color: #97E1F1; font-style: italic\">string<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #62E884\">get_string<\/span><span style=\"color: #F6F6F4\">()<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    <\/span><span style=\"color: #F286C4\">return<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #DEE492\">&quot;<\/span><span style=\"color: #E7EE98\">hello<\/span><span style=\"color: #DEE492\">&quot;<\/span><span style=\"color: #F6F6F4\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">}<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F286C4\">int<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #62E884\">main<\/span><span style=\"color: #F6F6F4\">()<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    std<\/span><span style=\"color: #F286C4\">::<\/span><span style=\"color: #F6F6F4\">string s <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #DEE492\">&quot;<\/span><span style=\"color: #E7EE98\">hello world<\/span><span style=\"color: #DEE492\">&quot;<\/span><span style=\"color: #F6F6F4\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    <\/span><span style=\"color: #62E884\">normal_forwarding<\/span><span style=\"color: #F6F6F4\">(s);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    <\/span><span style=\"color: #62E884\">perfect_forwarding<\/span><span style=\"color: #F6F6F4\">(s);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #7B7F8B\">    \/\/normal_forwarding(get_string());<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">}<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>\u4ee5\u4e0a<code>normal_forwarding<\/code>\u662f\u4e00\u4e2a\u5e38\u89c4\u7684\u8f6c\u53d1\u6a21\u7248\u51fd\u6570\uff0c\u5b9e\u9645\u4e0a\u8fdb\u884c\u7684\u662f\u503c\u7684\u4f20\u9012\uff0c\u6548\u7387\u4f4e\u4e0b\u3002\u90a3\u4e48\u6211\u4eec\u53ef\u4ee5\u600e\u4e48\u505a\u53bb\u63d0\u9ad8\u6548\u7387\uff1f\u4e00\u79cd\u65b9\u6cd5\u662f\u628a\u4ed6\u6539\u6210\u5f15\u7528\u4f20\u9012\uff0c\u5982<code>ref_forwarding<\/code>, \u4f46\u662f\u8fd9\u6837\u4fee\u6539\u7684\u95ee\u9898\u5728\u4e8e\uff0c\u5982\u679c\u4f20\u5165\u7684\u662f\u4e00\u4e2a\u53f3\u503c\uff0c\u4f1a\u6709\u7f16\u8bd1\u9519\u8bef\u3002<\/p>\n\n\n\n<p>\u4f46\u662f\u6ca1\u5173\u7cfb\uff0c\u6211\u4eec\u4e4b\u524d\u6709\u8bf4\u8fc7\uff0c\u5e38\u91cf\u5de6\u503c\u5f15\u7528\uff0c\u65e2\u53ef\u4ee5\u7ed1\u5b9a\u5de6\u503c\uff0c\u53c8\u53ef\u4ee5\u7ed1\u5b9a\u53f3\u503c\u3002\u6240\u4ee5\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5e38\u91cf\u5de6\u503c\u5f15\u7528\u53bb\u5f15\u7528\u53f3\u503c\u3002\u4f46\u662f\u8fd9\u4e5f\u6709\u95ee\u9898\uff0c\u90a3\u5c31\u662f\u867d\u7136\u5b83\u53ef\u4ee5\u201c\u5b8c\u7f8e\u201d\u5730\u8f6c\u53d1\u5b57\u7b26\u4e32\uff0c\u4f46\u662f\u5982\u679c\u5728\u540e\u7eed\u7684\u51fd\u6570\u4e2d\u9700\u8981\u4fee\u6539\u5b57\u7b26\u4e32\uff0c\u5219\u4f1a\u7f16\u8bd1\u9519\u8bef\u3002<\/p>\n\n\n\n<p>\u90a3\u4e48\u4e07\u80fd\u5f15\u7528\u5c31\u80fd\u89e3\u51b3\u4e0a\u8ff0\u7684\u95ee\u9898\u3002<code>perfect_forwarding<\/code>.\u4e07\u80fd\u5f15\u7528\u80fd\u591f\u6839\u636e\u503c\u7684\u7c7b\u578b\u81ea\u52a8\u63a8\u5bfc\u5f62\u53c2\uff0c\u5982\u679c\u5b9e\u53c2\u662f\u5de6\u503c\uff0c\u5219\u5f62\u53c2\u88ab\u63a8\u5bfc\u4e3a\u5de6\u503c\u5f15\u7528\uff0c\u5982\u679c\u5b9e\u53c2\u662f\u53f3\u503c\uff0c\u5219\u5f62\u53c2\u88ab\u63a8\u5bfc\u4e3a\u53f3\u503c\u5f15\u7528\u3002<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>std::forward&lt;T&gt;(t) \u7684\u672c\u8d28\u4e5f\u662fstatic_cast\uff0c\u53ea\u662f\u548cstd::move\u7684\u65b9\u5f0f\u4e00\u6837\u9690\u85cf\u4e86\u7ec6\u8282\u3002<\/p>\n<\/blockquote>\n\n\n\n<h3 class=\"wp-block-heading\">std::move \u548c std::forward\u7684\u533a\u522b<\/h3>\n\n\n\n<p>std::move \u4e00\u5b9a\u4f1a\u5c06\u5b9e\u53c2\u8f6c\u5316\u4e3a\u4e00\u4e2a\u53f3\u503c\u5f15\u7528\uff0c\u5e76\u4e14\u4f7f\u7528std::move\u4e0d\u9700\u8981\u6307\u5b9a\u6a21\u7248\u5b9e\u53c2\uff0c\u6a21\u7248\u5b9e\u53c2\u662f\u63a8\u5bfc\u51fa\u6765\u7684\u3002<\/p>\n\n\n\n<p>std::forward \u4f1a\u6839\u636e\u5de6\u503c\u548c\u53f3\u503c\u7684\u5b9e\u9645\u60c5\u51b5\u8fdb\u884c\u8f6c\u53d1\uff0c\u5728\u4f7f\u7528\u7684\u65f6\u5019\u9700\u8981\u6307\u5b9a\u6a21\u7248\u53c2\u6570\u3002<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">Question<\/h1>\n\n\n\n<ol start=\"1\" class=\"wp-block-list\">\n<li>++x\u8fd4\u56de\u7684\u662f\u5de6\u503c\u8fd8\u662f\u53f3\u503c\uff1fx++\u5462\uff1f<strong>\u5b57\u7b26\u4e32<\/strong><strong>\u5b57\u9762\u91cf<\/strong>\u5462\uff1f<\/li>\n<\/ol>\n\n\n\n<p>\u5148\u8bf4\u7ed3\u8bba\uff1a++x\u8fd4\u56de\u7684\u662f\u5de6\u503c\uff0cx++\u8fd4\u56de\u7684\u662f\u53f3\u503c\u3002\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\u901a\u5e38\u662f\u53f3\u503c\uff0c\u9664\u4e86\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\u4e4b\u5916\u3002<\/p>\n\n\n\n<p>++x\u662f\u5bf9x\u81ea\u589e\u540e\u9a6c\u4e0a\u8fd4\u56de\u5176\u81ea\u8eab\uff0c\u800cx++\u662f\u5148\u751f\u6210x\u7684\u4e34\u65f6\u590d\u5236\u7136\u540e\u624d\u5bf9x\u9012\u589e\uff0c\u6700\u540e\u8fd4\u56de\u4e34\u65f6\u590d\u5236\u5185\u5bb9\u3002<\/p>\n\n\n\n<p>\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\u901a\u5e38\u662f\u53f3\u503c\uff0c\u9664\u4e86\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\u4e4b\u5916\uff1a<\/p>\n\n\n\n<p>\u4ee5\u4e0b\u4ee3\u7801\u53ef\u4ee5\u88ab\u7f16\u8bd1\u6210\u529f\u3002<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>auto p = &amp;&#8221;this is a lvalue&#8221;;<\/p>\n<\/blockquote>\n\n\n\n<p>\u8ba9\u6211\u4eec\u60f3\u60f3\u8fd9\u662f\u4e3a\u4ec0\u4e48\uff1f\u56e0\u4e3a\u7f16\u8bd1\u5668\u4f1a\u5c06\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\u5b58\u50a8\u5230\u7a0b\u5e8f\u7684\u6570\u636e\u6bb5\u4e2d\uff0c\u7a0b\u5e8f\u52a0\u8f7d \u7684\u65f6\u5019\u4e5f\u4f1a\u4e3a\u5176\u5f00\u8f9f\u5185\u5b58\u7a7a\u95f4\uff0c\u6240\u4ee5\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u53d6\u5730\u5740\u7b26&amp;\u6765\u83b7\u53d6 \u5b57\u7b26\u4e32\u5b57\u9762\u91cf\u7684\u5185\u5b58\u5730\u5740\u3002<\/p>\n\n\n\n<ol start=\"2\" class=\"wp-block-list\">\n<li>\u4ec0\u4e48\u662f\u5de6\u503c\u5f15\u7528\uff0c\u4ec0\u4e48\u662f\u53f3\u503c\u5f15\u7528\uff1f<\/li>\n<\/ol>\n\n\n\n<p>\u5148\u8bf4\u7ed3\u8bba\uff1a\u5de6\u503c\u5f15\u7528\uff1a\u5de6\u503c\u5f15\u7528\u662f\u6307\u5411\u5de6\u503c\u7684\u5f15\u7528\u3002\u5de6\u503c\u5f15\u7528\u53ea\u80fd\u7528\u5355\u4e2a\u7b26\u53f7&amp;\u8868\u793a\u3002<strong>\u5e38\u91cf\u5de6\u503c\u5f15\u7528<\/strong>\u53ef\u4ee5\u7ed1\u5b9a\u5de6\u503c\uff0c\u4e5f\u53ef\u4ee5\u7ed1\u5b9a\u53f3\u503c\uff0c<strong>\u975e\u5e38\u91cf\u5de6\u503c\u5f15\u7528\u53ea\u80fd\u7ed1\u5b9a\u5de6\u503c.<\/strong><\/p>\n\n\n\n<p>\u53f3\u503c\u5f15\u7528\uff1a\u53f3\u503c\u5f15\u7528\uff1a\u53f3\u503c\u5f15\u7528\u662f\u6307\u5411\u53f3\u503c\u7684\u5f15\u7528\u3002\u53f3\u503c\u5f15\u7528\u4f7f\u7528\u4e24\u4e2a\u7b26\u53f7&amp;&amp;\u8868\u793a\uff0c\u5373&amp;&amp;\u3002\u53f3\u503c\u5f15\u7528\u7684\u4e3b\u8981\u4f5c\u7528\u662f\u652f\u6301<strong>\u79fb\u52a8\u8bed\u4e49<\/strong>\u548c<strong>\u5b8c\u7f8e\u8f6c\u53d1<\/strong>\u3002\u901a\u8fc7\u79fb\u52a8\u8bed\u4e49\uff0c\u6211\u4eec\u53ef\u4ee5\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u64cd\u4f5c\uff0c\u63d0\u9ad8\u6027\u80fd\u3002<\/p>\n\n\n\n<ol start=\"3\" class=\"wp-block-list\">\n<li>\u53f3\u503c\u5f15\u7528\u5982\u4f55\u63d0\u9ad8\u6027\u80fd\uff1f<\/li>\n<\/ol>\n\n\n\n<p>\u5148\u8bf4\u7ed3\u8bba\uff1a\u53f3\u503c\u5f15\u7528\u901a\u8fc7\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u62f7\u8d1d\u6765\u63d0\u5347\u6027\u80fd\u3002<\/p>\n\n\n\n<p>\u4e3e\u4e24\u4e2a\u4f8b\u5b50\uff1a<\/p>\n\n\n\n<ol start=\"1\" class=\"wp-block-list\">\n<li>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u53f3\u503c\u5f15\u7528\u6765\u63a5\u6536\u4e00\u4e2a\u51fd\u6570\u8fd4\u56de\u7684\u4e34\u65f6\u5bf9\u8c61\uff0c\u8fd9\u65f6\u5019\u5c40\u90e8\u53d8\u91cf\u7684\u58f0\u660e\u671f\u4f1a\u88ab\u5ef6\u957f\uff0c\u53f3\u503c\u5f15\u7528\u4f1a\u76f4\u63a5\u4f7f\u7528\u5c40\u90e8\u5bf9\u8c61\u7684\u5185\u5b58\uff0c\u907f\u514d\u4e86\u4f7f\u7528\u8fd9\u4e2a\u5c40\u90e8\u53d8\u91cf\u521b\u5efa\u4e00\u4e2a\u4e34\u65f6\u5bf9\u8c61\u6240\u5e26\u6765\u7684\u4e0d\u5fc5\u8981\u7684\u5f00\u9500\u3002<\/li>\n\n\n\n<li>\u5728\u4f7f\u7528\u4e00\u4e2a\u4e34\u65f6\u5bf9\u8c61\u6784\u5efa\u4e00\u4e2a\u5bf9\u8c61\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u79fb\u52a8\u6784\u9020\u51fd\u6570\uff0c\u4f7f\u6211\u4eec\u5728\u6784\u9020\u65b0\u5bf9\u8c61\u65f6\uff0c\u5c06\u4e00\u4e2a\u8d44\u6e90\u4ece\u4e00\u4e2a\u4e34\u65f6\u5bf9\u8c61\uff08\u53f3\u503c\uff09&#8221;\u79fb\u52a8&#8221;\u5230\u65b0\u5bf9\u8c61\uff0c\u800c\u4e0d\u662f\u521b\u5efa\u65b0\u5bf9\u8c61\u7684\u62f7\u8d1d\u3002\u8fd9\u6837\u53ef\u4ee5\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u62f7\u8d1d\u64cd\u4f5c\u3002<\/li>\n<\/ol>\n\n\n\n<ol start=\"4\" class=\"wp-block-list\">\n<li>\u4ecb\u7ecd\u4e00\u4e0bRVO\uff1f<\/li>\n<\/ol>\n\n\n\n<p>RVO\uff08Return Value Optimization\uff09\u662f\u4e00\u79cd\u7f16\u8bd1\u5668\u4f18\u5316\u6280\u672f\uff0c\u7528\u4e8e\u6d88\u9664\u4e0d\u5fc5\u8981\u7684\u4e34\u65f6\u5bf9\u8c61\u62f7\u8d1d\uff0c\u4ece\u800c\u63d0\u9ad8\u4ee3\u7801\u7684\u6027\u80fd\u3002<\/p>\n\n\n\n<p>RVO\u7684\u57fa\u672c\u601d\u60f3\u662f\uff1a\u5728\u51fd\u6570\u8c03\u7528\u6808\u4e0a\u76f4\u63a5\u6784\u9020\u8fd4\u56de\u503c\uff0c\u800c\u4e0d\u662f\u5148\u6784\u9020\u4e00\u4e2a\u5c40\u90e8\u5bf9\u8c61\uff0c\u7136\u540e\u518d\u62f7\u8d1d\u5230\u8c03\u7528\u8005\u7684\u6808\u7a7a\u95f4\u3002\u8fd9\u6837\u53ef\u4ee5\u51cf\u5c11\u4e34\u65f6\u5bf9\u8c61\u7684\u521b\u5efa\u548c\u9500\u6bc1\u3002<\/p>\n\n\n\n<ol start=\"5\" class=\"wp-block-list\">\n<li>\u79fb\u52a8\u6784\u9020\u51fd\u6570\u53ef\u80fd\u6709\u4ec0\u4e48\u95ee\u9898\uff1f<\/li>\n<\/ol>\n\n\n\n<p>\u5728\u79fb\u52a8\u6784\u9020\u51fd\u6570\u4e2d\uff0c\u5982\u679c\u4e00\u4e2a\u5bf9\u8c61\u7684\u8d44\u6e90\u79fb\u52a8\u5230\u53e6\u4e00\u4e2a\u5bf9\u8c61\u65f6\u53d1\u751f\u4e86\u5f02\u5e38\uff0c\u4e5f\u5c31\u662f\u8bf4\u5bf9\u8c61\u7684\u4e00\u90e8\u5206\u8f6c\u4e49\u4e86\u4f46\u662f\u53e6\u4e00\u90e8\u5206\u6ca1\u6709\uff0c\u5219\u4f1a\u6709\u79fb\u52a8\u4e0d\u5b8c\u6574\u7684\u73b0\u8c61\u53d1\u751f\uff0c\u8fd9\u79cd\u662f\u672a\u5b9a\u4e49\u884c\u4e3a\u3002\u6240\u4ee5\u7f16\u5199\u79fb\u52a8\u6784\u9020\u51fd\u6570\u65f6\u8981\u4fdd\u8bc1\u4e0d\u629b\u51fa\u5f02\u5e38\u3002<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">\u53c2\u8003\u8d44\u6599<\/h1>\n\n\n\n<ol start=\"1\" class=\"wp-block-list\">\n<li>\u73b0\u4ee3C++\u8bed\u8a00\u6838\u5fc3\u7279\u6027\u89e3\u6790<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>\u4ec0\u4e48\u662f\u5de6\u503c\uff0c\u4ec0\u4e48\u662f\u53f3\u503c\uff1f \u5de6\u503c\uff1a\u5177\u6709\u751f\u547d\u5468\u671f\u7684\uff0c\u5177\u6709\u540d\u79f0\u7684\u503c\uff0c\u5373\u4e3a\u5b83\u6709\u4e00\u4e2a\u5177\u4f53\u7684\u5185\u5b58\u7a7a\u95f4\u3002 \u53f3\u503c\uff1a\u6ca1\u6709\u751f\u547d\u5468\u671f [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":397,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,1,3],"tags":[78,60,59],"class_list":["post-459","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-c","category-uncategorized","category-coding_language","tag-cpp","tag-60","tag-59"],"_links":{"self":[{"href":"http:\/\/yangnanbei.top\/index.php\/wp-json\/wp\/v2\/posts\/459"}],"collection":[{"href":"http:\/\/yangnanbei.top\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/yangnanbei.top\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/yangnanbei.top\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/yangnanbei.top\/index.php\/wp-json\/wp\/v2\/comments?post=459"}],"version-history":[{"count":1,"href":"http:\/\/yangnanbei.top\/index.php\/wp-json\/wp\/v2\/posts\/459\/revisions"}],"predecessor-version":[{"id":463,"href":"http:\/\/yangnanbei.top\/index.php\/wp-json\/wp\/v2\/posts\/459\/revisions\/463"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/yangnanbei.top\/index.php\/wp-json\/wp\/v2\/media\/397"}],"wp:attachment":[{"href":"http:\/\/yangnanbei.top\/index.php\/wp-json\/wp\/v2\/media?parent=459"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/yangnanbei.top\/index.php\/wp-json\/wp\/v2\/categories?post=459"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/yangnanbei.top\/index.php\/wp-json\/wp\/v2\/tags?post=459"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}