{"id":6544,"date":"2026-10-10T09:55:13","date_gmt":"2026-10-10T01:55:13","guid":{"rendered":"https:\/\/blog.coolcoding.cn\/?p=6544"},"modified":"2026-10-10T10:19:57","modified_gmt":"2026-10-10T02:19:57","slug":"%e9%9b%b6%e5%bc%80%e9%94%80%e6%8a%bd%e8%b1%a1","status":"publish","type":"post","link":"https:\/\/blog.coolcoding.cn\/?p=6544","title":{"rendered":"\u96f6\u5f00\u9500\u62bd\u8c61"},"content":{"rendered":"\n<p><strong>\u62bd\u8c61\u5c42\u5728\u7f16\u8bd1\u671f\u5c31\u88ab\u5c55\u5f00\u3001\u6d88\u9664\uff0c\u8fd0\u884c\u65f6\u4e0d\u4f1a\u989d\u5916\u589e\u52a0\u6307\u4ee4\u3001\u4e0d\u4f1a\u5f15\u5165\u95f4\u63a5\u8df3\u8f6c\u3001\u6ca1\u6709\u989d\u5916\u5185\u5b58\u5f00\u9500\uff0c\u9ad8\u7ea7\u62bd\u8c61\u4ee3\u7801\u7f16\u8bd1\u540e\u7b49\u4ef7\u4e8e\u624b\u5199\u7684\u5e95\u5c42\u4ee3\u7801\u3002<\/strong><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>\u4ee5C#\u3001C++\u4e3e\u4f8b<\/p>\n\n\n\n<ul>\n<li>C++ \u6a21\u677f\uff1a<strong>\u7f16\u8bd1\u671f\u5b9e\u4f8b\u5316<\/strong>\uff0c\u6bcf\u79cd\u4e0d\u540c\u7c7b\u578b\u751f\u6210\u4e00\u4efd\u72ec\u7acb\u673a\u5668\u7801\uff0c\u6ca1\u6709\u8fd0\u884c\u65f6\u7c7b\u578b\u5206\u53d1<\/li>\n\n\n\n<li>C# \u6cdb\u578b\uff1a\u503c\u7c7b\u578b\u4f1a\u5355\u72ec\u5b9e\u4f8b\u5316\uff0c\u4f46<strong>\u5f15\u7528\u7c7b\u578b\u5171\u4eab\u540c\u4e00\u4efd IL \u5b9e\u73b0<\/strong>\uff0c\u4f9d\u8d56\u8fd0\u884c\u65f6\u7c7b\u578b\u68c0\u67e5\u3001\u865a\u65b9\u6cd5\u8868\uff0c\u5b58\u5728\u5f00\u9500<\/li>\n<\/ul>\n\n\n\n<p><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">C# \u63a5\u53e3\u793a\u4f8b\u4ee3\u7801<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>using System;\n\ninterface ICalc\n{\n    int Add(int a, int b);\n}\n\nstruct CalcImpl : ICalc\n{\n    public int Add(int a, int b)\n    {\n        return a + b;\n    }\n}\n\n\/\/ \u6cdb\u578b\u65b9\u6cd5\uff0c\u63a5\u6536\u63a5\u53e3\nstatic int Run&lt;T>(T calc, int x, int y) where T : ICalc\n{\n    return calc.Add(x, y);\n}\n\npublic static void Main()\n{\n    CalcImpl c = new CalcImpl();\n    Console.WriteLine(Run(c, 1, 2));\n}\n\n\u5bf9\u5e94\u7684\u5b57\u8282\u7801\n.method private hidebysig static int32  Run&lt;T>(!!T calc, int32 x, int32 y) cil managed\n{\n  .paramtype !T\n  .maxstack  3\n  .locals init (&#91;0] class ICalc)\n  \/\/ \u88c5\u7bb1, \u628a\u503c\u7c7b\u578b T \u8f6c\u4e3a ICalc \u5f15\u7528\n  box !!T\n  stloc.0\n  ldloc.0\n  ldloc.1\n  ldloc.2\n  \/\/ \u63a5\u53e3\u865a\u8c03\u7528 callvirt\uff0c\u95f4\u63a5\u8df3\u8f6c\n  callvirt instance int32 ICalc::Add(int32, int32)\n  ret\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">C++\u63a5\u53e3\u793a\u4f8b\u4ee3\u7801<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;\n\nstruct CalcImpl\n{\n    int Add(int a, int b) const\n    {\n        return a + b;\n    }\n};\n\n\ntemplate&lt;typename T&gt;\nint Run(const T&amp; calc, int x, int y)\n{\n    return calc.Add(x, y);\n}\n\nint main()\n{\n    CalcImpl c;\n    std::cout &lt;&lt; Run(c, 1, 2);\n    return 0;\n}\n\nASM:\n\nmain:\n        mov     esi, 2\n        mov     edi, 1\n        call    Run&lt;CalcImpl&gt;\n        mov     edi, eax\n        call    std::basic_ostream&lt;char&gt;::operator&lt;&lt;(int)\n        xor     eax, eax\n        ret\n\nRun&lt;CalcImpl&gt;:\n        lea     eax, &#91;rdi+rsi]  ; \u76f4\u63a5 a+b\uff0c\u65e0\u95f4\u63a5\u8df3\u8f6c\n        ret\n<\/code><\/pre>\n\n\n\n<p>C#: <code>callvirt<\/code> \u5c31\u662f<strong>\u865a\u8c03\u7528<\/strong>\uff0c\u8fd0\u884c\u65f6\u67e5\u8868\uff1b<code>box<\/code> \u5728\u5806\u5206\u914d\u5185\u5b58\uff0c\u8fd9\u5c31\u662f<strong>\u62bd\u8c61\u5e26\u6765\u7684\u8fd0\u884c\u65f6\u5f00\u9500<\/strong>\u3002<\/p>\n\n\n\n<p>C++:\u7f16\u8bd1\u5668\u5b9e\u4f8b\u5316 <code>Run&lt;CalcImpl><\/code>\uff0c<strong>\u7f16\u8bd1\u671f\u76f4\u63a5\u5c55\u5f00<\/strong>\uff1b<code>calc.Add<\/code> \u662f<strong>\u9759\u6001\u7ed1\u5b9a\uff08\u9759\u6001\u8c03\u7528\uff09<\/strong>\uff0c\u6ca1\u6709\u865a\u8868\u3001\u6ca1\u6709\u95f4\u63a5\u8df3\u8f6c\uff1b\u6ca1\u6709\u5806\u5206\u914d\u3001\u6ca1\u6709\u88c5\u7bb1\uff1b\u6700\u7ec8\u751f\u6210\u7684\u673a\u5668\u7801\uff0c\u7b49\u4ef7\u4e8e\u76f4\u63a5\u624b\u5199 <code>return 1+2<\/code>\uff1b<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">C# \u8fed\u4ee3\u5668\u793a\u4f8b<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>using System;\r\nusing System.Collections.Generic;\r\n\r\nclass Program\r\n{\r\n    static void Main()\r\n    {\r\n        List&lt;int> list = new List&lt;int> { 1, 2, 3, 4 };\r\n        int sum = 0;\r\n        foreach (var num in list)\r\n        {\r\n            sum += num;\r\n        }\r\n        Console.WriteLine(sum);\r\n    }\r\n}\n\n---\n\nList&lt;int> list = new List&lt;int>{1,2,3,4};\r\nint sum = 0;\r\nList&lt;int>.Enumerator enumerator = list.GetEnumerator();\r\nwhile (enumerator.MoveNext())\r\n{\r\n    sum += enumerator.Current;\r\n}\r\nenumerator.Dispose();\r\n\n\n---\n\n\r.method private hidebysig static void  Main() cil managed\r\n{\r\n  .entrypoint\r\n  .maxstack  3\r\n  .locals init (&#91;0] class &#91;mscorlib]System.Collections.Generic.List`1&lt;int32> list,\r\n           &#91;1] int32 sum,\r\n           &#91;2] valuetype &#91;mscorlib]System.Collections.Generic.List`1&lt;int32>\/Enumerator enumerator,\r\n           &#91;3] bool V_3)\r\n  \/\/ \u521b\u5efaList\r\n  newobj instance void class &#91;mscorlib]System.Collections.Generic.List`1&lt;int32>::.ctor()\r\n  stloc.0\r\n  \/\/ Add\u5143\u7d20\u7701\u7565\r\n  ldloc.0\r\n  callvirt instance valuetype &#91;mscorlib]System.Collections.Generic.List`1&lt;int32>\/Enumerator class &#91;mscorlib]System.Collections.Generic.List`1&lt;int32>::GetEnumerator()\r\n  stloc.2\r\n  br.s       IL_002a\r\nIL_0024:\r\n  ldloca.s   enumerator\r\n  call instance int32 valuetype &#91;mscorlib]System.Collections.Generic.List`1&lt;int32>\/Enumerator::get_Current()\r\n  add\r\n  stloc.1\r\nIL_002a:\r\n  ldloca.s   enumerator\r\n  call instance bool valuetype &#91;mscorlib]System.Collections.Generic.List`1&lt;int32>\/Enumerator::MoveNext()\r\n  stloc.3\r\n  ldloc.3\r\n  brtrue.s   IL_0024\r\n  \/\/ Dispose\r\n  ldloca.s   enumerator\r\n  constrained. valuetype &#91;mscorlib]System.Collections.Generic.List`1&lt;int32>\/Enumerator\r\n  callvirt instance void &#91;mscorlib]System.IDisposable::Dispose()\r\n  \r\n  \/\/ Console.WriteLine\u7701\u7565\r\n  ret\r\n}\r\n\n\n1. constrained. + callvirt\uff1a\u865a\u8c03\u7528\uff0c\u7528\u6765\u8c03\u7528 IDisposable.Dispose()\u3002\u8d70\u865a\u8c03\u7528\u5206\u652f\n2. \u8bed\u6cd5\u7cd6\u5f3a\u5236\u9075\u5faa IEnumerator\uff0c\u62bd\u8c61\u63a5\u53e3\u662f\u56fa\u5b9a\u89c4\u8303\uff1b\n3. \u628a List&lt;int>`\u8d4b\u503c\u7ed9 IEnumerable&lt;int>\u53d8\u91cf\u518d foreach\uff1a\nIEnumerable&lt;int> seq = new List&lt;int>{1,2,3};\nforeach(var n in seq) sum += n;\n\u6b64\u65f6 GetEnumerator()\u8fd4\u56de\u7684\u679a\u4e3e\u5668 \u4f1a\u88c5\u7bb1\u5230\u5806\u4e0a\uff0c\u4ea7\u751f GC \u5206\u914d\uff0c\u5f00\u9500\u53d8\u5927\nC# foreach \u662f \u57fa\u4e8e\u63a5\u53e3\u7684\u62bd\u8c61\uff0c\u62bd\u8c61\u5c42\u5728\u8fd0\u884c\u65f6\u5b58\u5728\u3002\u4e00\u65e6\u5411\u4e0a\u8f6c\u578b\u4e3a\u63a5\u53e3\uff0c\u5c31\u4f1a\u88c5\u7bb1\u3002\n\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">C++\u8fed\u4ee3\u5668\u793a\u4f8b<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream>\n#include &lt;vector>\n\nint main()\n{\n    std::vector&lt;int> vec = {1,2,3,4};\n    int sum = 0;\n    for(auto num : vec)\n    {\n        sum += num;\n    }\n    std::cout &lt;&lt; sum;\n    return 0;\n}\n\n---\n\nauto &amp;&amp;range = vec;\nauto it = range.begin();\nauto end = range.end();\nfor(; it != end; ++it)\n{\n    auto num = *it;\n    sum += num;\n}\n\n---\n\nmain:\n        push    rbx\n        mov     edi, 16\n        call    operator new\n        mov     DWORD PTR &#91;rax], 1\n        mov     DWORD PTR &#91;rax+4], 2\n        mov     DWORD PTR &#91;rax+8], 3\n        mov     DWORD PTR &#91;rax+12], 4\n        mov     esi, 10\n        mov     rdi, rax\n        call    std::basic_ostream&lt;char>::operator&lt;&lt;(int)\n        mov     rdi, rax\n        call    std::basic_ostream&lt;char>::flush()\n        xor     eax, eax\n        pop     rbx\n        ret\n\nstd::vector::iterator \u662f\u6a21\u677f\u7c7b\u578b\uff0c\u4e0d\u7ee7\u627f\u4efb\u4f55\u63a5\u53e3\u7c7b\n\u8fed\u4ee3\u5668\u5bf9\u8c61\u5f88\u5c0f\uff0c\u6808\u5206\u914d\uff0c\u65e0\u5806\u5206\u914d\n\u62bd\u8c61\uff08\u8fed\u4ee3\u5668\u3001range-for \u8bed\u6cd5\uff09\u5728\u7f16\u8bd1\u671f\u6d88\u5931\uff1b\u8fd0\u884c\u65f6\u4ee3\u7801\u7b49\u4ef7\u4e8e\u624b\u5199\u6307\u9488\u904d\u5386\u6570\u7ec4,\u96f6\u5f00\u9500\u62bd\u8c61<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<ul>\n<li>\u4f60\u6ca1\u7528\u5230\u7684\u4e1c\u897f\uff0c\u4f60\u4e0d\u9700\u8981\u4e3a\u4e4b\u4ed8\u51fa\u4ee3\u4ef7\uff08\u6ca1\u6709\u9690\u85cf\u7684\u8fd0\u884c\u65f6\u5f00\u9500\uff09\u3002<\/li>\n\n\n\n<li> \u4f60\u7528\u5230\u7684\u4e1c\u897f\uff0c\u4f60\u81ea\u5df1\u624b\u5199\u6c47\u7f16\u4ee3\u7801\u4e5f\u4e0d\u53ef\u80fd\u505a\u5f97\u66f4\u597d\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>C#\u7684\u95ee\u9898<\/p>\n\n\n\n<ol>\n<li><strong>\u8fb9\u754c\u68c0\u67e5 (Bounds Checking)\uff1a<\/strong>&nbsp;\u6bcf\u6b21\u4f60\u8bbf\u95ee C# \u6570\u7ec4&nbsp;<code>arr[i]<\/code>\uff0cCLR \u90fd\u4f1a\u5077\u5077\u68c0\u67e5&nbsp;<code>i<\/code>&nbsp;\u662f\u5426\u8d8a\u754c\u3002\u8fd9\u4fdd\u62a4\u4e86\u7a0b\u5e8f\uff0c\u4f46\u6d88\u8017\u4e86 CPU \u5468\u671f\u3002C++ \u9ed8\u8ba4\u4e0d\u68c0\u67e5\uff0c\u5982\u679c\u4f60\u8981\u6781\u81f4\u901f\u5ea6\uff0c\u5b83\u5c31\u7ed9\u4f60\u6781\u81f4\u901f\u5ea6\u3002<\/li>\n\n\n\n<li><strong>\u865a\u51fd\u6570\u8868 (VTable)\uff1a<\/strong>&nbsp;C# \u7684\u65b9\u6cd5\u9ed8\u8ba4\u5f88\u591a\u662f\u865a\u7684\uff08\u6216\u8005\u8bf4\u4e3a\u4e86\u591a\u6001\u6027\u505a\u4e86\u5f88\u591a\u51c6\u5907\uff09\u3002C++ \u9664\u975e\u4f60\u663e\u5f0f\u58f0\u660e&nbsp;<code>virtual<\/code>\uff0c\u5426\u5219\u51fd\u6570\u8c03\u7528\u5c31\u662f\u4e00\u6b21\u7b80\u5355\u7684\u5730\u5740\u8df3\u8f6c\u3002<\/li>\n\n\n\n<li><strong>\u5783\u573e\u56de\u6536 (GC)\uff1a<\/strong>&nbsp;\u4e3a\u4e86\u7ba1\u7406\u5185\u5b58\uff0cC# \u5bf9\u8c61\u5934\u91cc\u5fc5\u987b\u5305\u542b\u4e00\u4e9b\u5143\u6570\u636e\uff08\u6bd4\u5982\u540c\u6b65\u5757\u7d22\u5f15\u3001\u7c7b\u578b\u6307\u9488\uff09\u3002\u6bcf\u4e2a\u5bf9\u8c61\u90fd\u6bd4 C++ \u7684\u7ed3\u6784\u4f53\u201c\u80d6\u201d\u90a3\u4e48\u4e00\u70b9\u3002<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>\u96f6\u62bd\u8c61\u6210\u672c\uff08Zero-Cost Abstraction\uff09<\/strong>\u00a0\u662f\u4e00\u79cd\u6838\u5fc3\u8bbe\u8ba1\u7406\u5ff5\uff0c\u6307\u00a0<strong>\u9ad8\u7ea7\u8bed\u8a00\u7279\u6027\uff08\u5982\u7c7b\u3001\u6a21\u677f\u3001RAII\u00a0\u7b49\uff09\u5728\u8fd0\u884c\u65f6\u4e0d\u4f1a\u5f15\u5165\u989d\u5916\u5f00\u9500<\/strong>\u3002\u8fd9\u610f\u5473\u7740\u5f00\u53d1\u8005\u53ef\u4ee5\u7f16\u5199\u9ad8\u53ef\u8bfb\u6027\u3001\u9ad8\u7ef4\u62a4\u6027\u7684\u4ee3\u7801\uff0c\u540c\u65f6\u4eab\u53d7\u4e0e\u624b\u5199\u5e95\u5c42\u4ee3\u7801\u76f8\u540c\u7684\u6027\u80fd\u3002<\/p>\n\n\n\n<p>\u96f6\u62bd\u8c61\u6210\u672c\u7684\u6838\u5fc3\u662f\u00a0<strong>\u201c\u4e0d\u4e3a\u672a\u4f7f\u7528\u7684\u529f\u80fd\u4ed8\u8d39\u201d<\/strong>\uff1a<\/p>\n\n\n\n<p> &#8211;\u00a0<strong>\u7f16\u8bd1\u65f6\u4f18\u5316<\/strong>\uff1a\u62bd\u8c61\u903b\u8f91\u5728\u7f16\u8bd1\u671f\u95f4\u88ab\u5904\u7406\uff0c\u8fd0\u884c\u65f6\u65e0\u989d\u5916\u5f00\u9500\u3002 <\/p>\n\n\n\n<p>&#8211;\u00a0<strong>\u7b49\u4ef7\u673a\u5668\u7801<\/strong>\uff1a\u62bd\u8c61\u751f\u6210\u7684\u4ee3\u7801\u4e0e\u624b\u52a8\u4f18\u5316\u7684\u5e95\u5c42\u4ee3\u7801\u6548\u7387\u76f8\u540c\u3002 <\/p>\n\n\n\n<p>&#8211;\u00a0<strong>\u6027\u80fd\u53ef\u63a7<\/strong>\uff1a\u5f00\u53d1\u8005\u65e0\u9700\u4e3a\u4e86\u6027\u80fd\u727a\u7272\u4ee3\u7801\u7ed3\u6784\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>C++\u7684\u5b9e\u73b0\u624b\u6bb5\uff1a<\/p>\n\n\n\n<p>constexpr<\/p>\n\n\n\n<p>inline<\/p>\n\n\n\n<p>\u6a21\u677f\u5143\u7f16\u7a0b<\/p>\n\n\n\n<p>RAII<\/p>\n\n\n\n<p>\u79fb\u52a8\u8bed\u4e49<\/p>\n\n\n\n<p>\u7f16\u8bd1\u65f6\u591a\u6001<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u62bd\u8c61\u5c42\u5728\u7f16\u8bd1\u671f\u5c31\u88ab\u5c55\u5f00\u3001\u6d88\u9664\uff0c\u8fd0\u884c\u65f6\u4e0d\u4f1a\u989d\u5916\u589e\u52a0\u6307\u4ee4\u3001\u4e0d\u4f1a\u5f15\u5165\u95f4\u63a5\u8df3\u8f6c\u3001\u6ca1\u6709\u989d\u5916\u5185\u5b58\u5f00\u9500\uff0c\u9ad8\u7ea7\u62bd\u8c61\u4ee3\u7801\u7f16\u8bd1\u540e\u7b49 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"hide_page_title":"","footnotes":""},"categories":[28,1],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/posts\/6544"}],"collection":[{"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6544"}],"version-history":[{"count":19,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/posts\/6544\/revisions"}],"predecessor-version":[{"id":6564,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/posts\/6544\/revisions\/6564"}],"wp:attachment":[{"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6544"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6544"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6544"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}