{"id":1359,"date":"2025-02-04T00:57:12","date_gmt":"2025-02-03T16:57:12","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=1359"},"modified":"2025-02-04T00:57:13","modified_gmt":"2025-02-03T16:57:13","slug":"c%e6%95%99%e7%a8%8b%e4%b9%8b%ef%bc%9a6-c11%e5%8f%8a%e4%bb%a5%e5%90%8e%e6%96%b0%e7%89%b9%e6%80%a7","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/02\/04\/c%e6%95%99%e7%a8%8b%e4%b9%8b%ef%bc%9a6-c11%e5%8f%8a%e4%bb%a5%e5%90%8e%e6%96%b0%e7%89%b9%e6%80%a7\/","title":{"rendered":"C++11\u53ca\u4ee5\u540e\u7279\u6027\uff1a\u65b0\u8bed\u6cd5\u4e0e\u65b0\u529f\u80fd"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">C++11\u53ca\u540e\u7eed\u7248\u672c\uff08C++14\u3001C++17\u3001C++20\uff09\u5f15\u5165\u4e86\u8bb8\u591a\u65b0\u7684\u7279\u6027\uff0c\u6781\u5927\u63d0\u5347\u4e86C++\u7684\u8868\u8fbe\u529b\u3001\u53ef\u8bfb\u6027\u548c\u6027\u80fd\u3002\u4e86\u89e3\u8fd9\u4e9b\u65b0\u7279\u6027\u5c06\u5e2e\u52a9\u5f00\u53d1\u8005\u66f4\u9ad8\u6548\u5730\u7f16\u5199\u73b0\u4ee3C++\u4ee3\u7801\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>C++11\u7684\u65b0\u589e\u7279\u6027<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>auto\u5173\u952e\u5b57<\/strong><br><code>auto<\/code>\u5141\u8bb8\u7f16\u8bd1\u5668\u6839\u636e\u8868\u8fbe\u5f0f\u7684\u7c7b\u578b\u63a8\u5bfc\u53d8\u91cf\u7684\u7c7b\u578b\u3002\u5b83\u8ba9\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\uff0c\u5c24\u5176\u5728\u5904\u7406\u590d\u6742\u7c7b\u578b\u65f6\u975e\u5e38\u6709\u7528\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  auto x = 10;  \/\/ x\u63a8\u5bfc\u4e3aint\n  auto y = 3.14;  \/\/ y\u63a8\u5bfc\u4e3adouble<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u57fa\u4e8e\u8303\u56f4\u7684for\u5faa\u73af<\/strong><br>\u57fa\u4e8e\u8303\u56f4\u7684<code>for<\/code>\u5faa\u73af\u7b80\u5316\u4e86\u5bb9\u5668\u904d\u5386\u7684\u5199\u6cd5\uff0c\u4e0d\u9700\u8981\u4f7f\u7528\u8fed\u4ee3\u5668\u6216\u4e0b\u6807\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  std::vector&lt;int&gt; vec = {1, 2, 3};\n  for (auto&amp; v : vec) {\n      std::cout &lt;&lt; v &lt;&lt; \" \";  \/\/ \u8f93\u51fa1 2 3\n  }<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u7c7b\u578b\u63a8\u65ad\u4e0e\u7c7b\u578b\u522b\u540d\uff08using\uff09<\/strong><br><code>using<\/code>\u5173\u952e\u5b57\u7528\u4e8e\u521b\u5efa\u7c7b\u578b\u522b\u540d\uff0c\u589e\u5f3a\u4e86\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  using IntVec = std::vector&lt;int&gt;;  \/\/ \u7c7b\u578b\u522b\u540d\n  IntVec v = {1, 2, 3};<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u521d\u59cb\u5316\u5217\u8868\uff08initializer_list\uff09<\/strong><br>\u521d\u59cb\u5316\u5217\u8868\u4f7f\u5f97\u6570\u7ec4\u6216\u5bb9\u5668\u7684\u521d\u59cb\u5316\u66f4\u52a0\u7b80\u6d01\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  std::vector&lt;int&gt; v = {1, 2, 3};  \/\/ \u4f7f\u7528\u521d\u59cb\u5316\u5217\u8868<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>nullptr\u5173\u952e\u5b57<\/strong><br><code>nullptr<\/code>\u662fC++11\u5f15\u5165\u7684\u7a7a\u6307\u9488\u5e38\u91cf\uff0c\u53d6\u4ee3\u4e86<code>NULL<\/code>\uff0c\u63d0\u9ad8\u4e86\u7c7b\u578b\u5b89\u5168\u6027\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  int* p = nullptr;  \/\/ p\u662f\u4e00\u4e2a\u7a7a\u6307\u9488<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u5f3a\u5236\u7c7b\u578b\u8f6c\u6362\uff08static_cast\u3001dynamic_cast\u3001reinterpret_cast\uff09<\/strong><br>C++11\u5f15\u5165\u4e86\u66f4\u4e25\u683c\u7684\u7c7b\u578b\u8f6c\u6362\u65b9\u5f0f\uff0c\u907f\u514d\u4e86\u9690\u5f0f\u8f6c\u6362\u5e26\u6765\u7684\u6f5c\u5728\u95ee\u9898\uff1a<\/li>\n\n\n\n<li><code>static_cast<\/code>\u7528\u4e8e\u7c7b\u578b\u4e4b\u95f4\u7684\u5b89\u5168\u8f6c\u6362\u3002<\/li>\n\n\n\n<li><code>dynamic_cast<\/code>\u7528\u4e8e\u5728\u7ee7\u627f\u5173\u7cfb\u4e2d\u8fdb\u884c\u7c7b\u578b\u8f6c\u6362\uff0c\u652f\u6301\u8fd0\u884c\u65f6\u68c0\u67e5\u3002<\/li>\n\n\n\n<li><code>reinterpret_cast<\/code>\u7528\u4e8e\u4f4e\u7ea7\u522b\u7684\u5f3a\u5236\u7c7b\u578b\u8f6c\u6362\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  double d = 3.14;\n  int i = static_cast&lt;int&gt;(d);  \/\/ \u5c06double\u8f6c\u4e3aint<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>C++14\u4e0eC++17\u7684\u65b0\u7279\u6027<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Lambda\u8868\u8fbe\u5f0f\u7684\u589e\u5f3a<\/strong><br>C++14\u5bf9Lambda\u8868\u8fbe\u5f0f\u8fdb\u884c\u4e86\u589e\u5f3a\uff0c\u652f\u6301\u6355\u83b7\u5916\u90e8\u53d8\u91cf\u65f6\u4f7f\u7528<code>mutable<\/code>\u3001\u8fd4\u56de\u7c7b\u578b\u63a8\u5bfc\u7b49\u529f\u80fd\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  auto add = &#91;](int a, int b) -&gt; int { return a + b; };  \/\/ \u8fd4\u56de\u7c7b\u578b\u63a8\u5bfc<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u667a\u80fd\u6307\u9488\u7684\u6539\u8fdb<\/strong><br>C++14\u5f15\u5165\u4e86<code>std::make_shared<\/code>\u548c<code>std::make_unique<\/code>\uff0c\u4f7f\u5f97\u521b\u5efa\u667a\u80fd\u6307\u9488\u66f4\u5b89\u5168\u3001\u66f4\u9ad8\u6548\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  auto p = std::make_unique&lt;int&gt;(10);  \/\/ \u521b\u5efaunique_ptr<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u51fd\u6570\u8fd4\u56de\u7c7b\u578b\u63a8\u5bfc<\/strong><br>\u5728C++14\u4e2d\uff0c\u51fd\u6570\u8fd4\u56de\u7c7b\u578b\u53ef\u4ee5\u901a\u8fc7<code>auto<\/code>\u6765\u63a8\u5bfc\uff0c\u7b80\u5316\u4e86\u4ee3\u7801\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  auto sum(int a, int b) {\n      return a + b;  \/\/ \u8fd4\u56de\u7c7b\u578b\u4e3aint\n  }<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>std::optional\u3001std::variant<\/strong><br>C++17\u5f15\u5165\u4e86<code>std::optional<\/code>\u548c<code>std::variant<\/code>\uff0c\u5206\u522b\u7528\u4e8e\u8868\u793a\u53ef\u80fd\u7f3a\u5931\u7684\u503c\u548c\u591a\u79cd\u7c7b\u578b\u7684\u503c\u3002<\/li>\n\n\n\n<li><code>std::optional<\/code>\u8868\u793a\u4e00\u4e2a\u53ef\u80fd\u4e3a\u7a7a\u7684\u503c\u3002<\/li>\n\n\n\n<li><code>std::variant<\/code>\u8868\u793a\u591a\u4e2a\u7c7b\u578b\u4e2d\u7684\u4e00\u4e2a\u503c\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  std::optional&lt;int&gt; opt = 10;  \/\/ \u8868\u793a\u4e00\u4e2a\u53ef\u80fd\u6ca1\u6709\u503c\u7684int\n  std::variant&lt;int, float&gt; v = 3.14f;  \/\/ \u53ef\u4ee5\u5b58\u50a8int\u6216float<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>C++20\u65b0\u7279\u6027<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u8303\u56f4\u5e93\uff08ranges\uff09<\/strong><br>C++20\u5f15\u5165\u4e86\u8303\u56f4\u5e93\uff0c\u5b83\u4e3a\u5bb9\u5668\u548c\u7b97\u6cd5\u63d0\u4f9b\u4e86\u66f4\u5f3a\u5927\u548c\u7075\u6d3b\u7684\u64cd\u4f5c\u65b9\u5f0f\u3002<code>ranges<\/code>\u4f7f\u5f97\u64cd\u4f5c\u5bb9\u5668\u66f4\u52a0\u76f4\u89c2\u548c\u7b80\u6d01\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  #include &lt;ranges&gt;\n  std::vector&lt;int&gt; v = {1, 2, 3, 4};\n  auto result = v | std::ranges::transform(&#91;](int x) { return x * 2; });<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u4e09\u65b9\u62fc\u5199\uff08Concepts\uff09<\/strong><br><code>Concepts<\/code>\u662fC++20\u65b0\u589e\u7684\u7279\u6027\uff0c\u7528\u4e8e\u5b9a\u4e49\u6a21\u677f\u53c2\u6570\u7684\u8981\u6c42\uff0c\u4f7f\u5f97\u6a21\u677f\u4ee3\u7801\u66f4\u52a0\u53ef\u8bfb\u3001\u6613\u4e8e\u7406\u89e3\u3002\u901a\u8fc7\u6982\u5ff5\u53ef\u4ee5\u7cbe\u786e\u5730\u63a7\u5236\u6a21\u677f\u7684\u7ea6\u675f\u6761\u4ef6\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  template &lt;typename T&gt;\n  concept Addable = requires(T a, T b) {\n      a + b;\n  };\n\n  template &lt;Addable T&gt;\n  T add(T a, T b) { return a + b; }<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u534f\u7a0b\uff08coroutines\uff09<\/strong><br>C++20\u5f15\u5165\u4e86\u534f\u7a0b\uff0c\u7b80\u5316\u4e86\u5f02\u6b65\u7f16\u7a0b\u3002\u534f\u7a0b\u4f7f\u5f97\u4ee3\u7801\u53ef\u4ee5\u5728\u7b49\u5f85\u64cd\u4f5c\uff08\u5982I\/O\uff09\u65f6\u6682\u505c\u6267\u884c\uff0c\u5e76\u5728\u64cd\u4f5c\u5b8c\u6210\u540e\u6062\u590d\u6267\u884c\uff0c\u907f\u514d\u4e86\u590d\u6742\u7684\u56de\u8c03\u548c\u72b6\u6001\u7ba1\u7406\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  #include &lt;coroutine&gt;\n\n  std::future&lt;int&gt; async_task() {\n      co_return 42;  \/\/ \u534f\u7a0b\u8fd4\u56de\u503c\n  }<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>std::format\u4e0estd::span\u7b49\u65b0\u6807\u51c6\u5e93<\/strong><\/li>\n\n\n\n<li><code>std::format<\/code>\uff1a\u63d0\u4f9b\u4e86\u7c7b\u4f3cPython\u683c\u5f0f\u5316\u7684\u5b57\u7b26\u4e32\u529f\u80fd\u3002<\/li>\n\n\n\n<li><code>std::span<\/code>\uff1a\u8868\u793a\u4e00\u4e2a\u6570\u7ec4\u89c6\u56fe\uff0c\u7528\u4e8e\u5904\u7406\u5e8f\u5217\u6570\u636e\u800c\u4e0d\u6d89\u53ca\u6240\u6709\u6743\u7ba1\u7406\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  #include &lt;format&gt;\n  std::string s = std::format(\"Hello, {}!\", \"World\");  \/\/ \u683c\u5f0f\u5316\u5b57\u7b26\u4e32<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u603b\u7ed3<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">C++11\u53ca\u5176\u540e\u7eed\u7248\u672c\uff08C++14\u3001C++17\u3001C++20\uff09\u5f15\u5165\u4e86\u8bb8\u591a\u5f3a\u5927\u7684\u7279\u6027\uff0c\u4f7f\u5f97C++\u7f16\u7a0b\u66f4\u52a0\u73b0\u4ee3\u5316\u548c\u9ad8\u6548\u3002<code>auto<\/code>\u3001Lambda\u8868\u8fbe\u5f0f\u3001\u667a\u80fd\u6307\u9488\u3001\u8303\u56f4\u5e93\u548c\u534f\u7a0b\u7b49\u7279\u6027\u5927\u5927\u63d0\u9ad8\u4e86C++\u7684\u8868\u8fbe\u80fd\u529b\u548c\u53ef\u7ef4\u62a4\u6027\u3002C++20\u7684\u5f15\u5165\u4f7f\u5f97\u6a21\u677f\u7f16\u7a0b\u3001\u5f02\u6b65\u64cd\u4f5c\u548c\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u53d8\u5f97\u66f4\u52a0\u76f4\u89c2\u3002\u901a\u8fc7\u638c\u63e1\u8fd9\u4e9b\u65b0\u7279\u6027\uff0c\u5f00\u53d1\u8005\u80fd\u591f\u7f16\u5199\u51fa\u66f4\u52a0\u7b80\u6d01\u3001\u5065\u58ee\u548c\u9ad8\u6548\u7684\u4ee3\u7801\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>C++11\u53ca\u540e\u7eed\u7248\u672c\uff08C++14\u3001C++17\u3001C++20\uff09\u5f15\u5165\u4e86\u8bb8\u591a\u65b0\u7684\u7279\u6027\uff0c\u6781\u5927\u63d0\u5347\u4e86C++\u7684\u8868\u8fbe\u529b\u3001\u53ef\u8bfb\u6027 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[59],"tags":[],"class_list":["post-1359","post","type-post","status-publish","format-standard","hentry","category-c"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1359","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=1359"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1359\/revisions"}],"predecessor-version":[{"id":1360,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1359\/revisions\/1360"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=1359"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=1359"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=1359"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}