{"id":2261,"date":"2023-10-07T20:02:09","date_gmt":"2023-10-07T12:02:09","guid":{"rendered":"https:\/\/courtship.top\/?p=2261"},"modified":"2023-10-07T20:02:09","modified_gmt":"2023-10-07T12:02:09","slug":"c-11-%e5%b0%be%e7%bd%ae%e8%bf%94%e5%9b%9e%e7%b1%bb%e5%9e%8b","status":"publish","type":"post","link":"https:\/\/courtship.top\/index.php\/2023\/10\/07\/c-11-%e5%b0%be%e7%bd%ae%e8%bf%94%e5%9b%9e%e7%b1%bb%e5%9e%8b\/","title":{"rendered":"C++ 11 \u5c3e\u7f6e\u8fd4\u56de\u7c7b\u578b"},"content":{"rendered":"\n<p>C++11 \u6807\u51c6\u4e2d\u5f15\u5165\u4e86\u5c3e\u7f6e\u8fd4\u56de\u7c7b\u578b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int foo()\n{\n    return 0;\n}\n\u53ef\u4ee5\u7b49\u4ef7\u4e3a\nauto foo() -> int\n{\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>\u5f53\u8fd4\u56de\u7c7b\u578b\u6bd4\u8f83\u590d\u6742\u7684\u65f6\u5019\uff0c\u8fd4\u56de\u7c7b\u578b\u540e\u7f6e\u53ef\u80fd\u4f1a\u662f\u4e00\u4e2a\u4e0d\u9519\u7684\u9009\u62e9\u3002\u4efb\u4f55\u51fd\u6570\u7684\u5b9a\u4e49\u90fd\u80fd\u4f7f\u7528\u5c3e\u7f6e\u8fd4\u56de\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u5c3e\u7f6e\u8fd4\u56de\u7ecf\u5178\u573a\u666f<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u8fd4\u56de\u6570\u7ec4\u6307\u9488<\/h3>\n\n\n\n<p>\u56e0\u4e3a\u6570\u7ec4\u4e0d\u80fd\u88ab\u62f7\u8d1d\uff0c\u6240\u4ee5\u51fd\u6570\u4e0d\u80fd\u8fd4\u56de\u6570\u7ec4\u3002\u4e0d\u8fc7\uff0c\u51fd\u6570\u53ef\u4ee5\u8fd4\u56de\u6570\u7ec4\u7684\u6307\u9488\u6216\u5e94\u7528\u3002\u867d\u7136\u4ece\u8bed\u6cd5\u4e0a\u6765\u8bf4\uff0c\u60f3\u8981\u5b9a\u4e49\u4e00\u4e2a\u8fd4\u56de\u6570\u7ec4\u7684\u6307\u9488\u6216\u5f15\u7528\u7684\u51fd\u6570\u6bd4\u8f83\u7e41\u7410\uff0c\u4f46\u662f\u6709\u4e00\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u7b80\u5316\u8fd9\u4e00\u4efb\u52a1\uff0c\u5176\u4e2d\u6700\u76f4\u63a5\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u7c7b\u578b\u522b\u540d\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>typedef int arrT&#91;10]; \/\/ arrT \u662f\u4e00\u4e2a\u7c7b\u578b\u522b\u540d\uff0c\u5b83\u8868\u793a\u7684\u7c7b\u578b\u662f\u542b\u670910\u4e2a\u6574\u6570\u7684\u6570\u7ec4\nusing arrT = int&#91;10]; \/\/ arrT \u7684\u7b49\u4ef7\u58f0\u660e\narrT* func(int i); \/\/ func \u8fd4\u56de\u4e00\u4e2a\u6307\u5411\u542b\u670910\u4e2a\u6574\u6570\u7684\u6570\u7ec4\u7684\u6307\u9488<\/code><\/pre>\n\n\n\n<p>\u5f53\u6211\u4eec\u628a\u51fd\u6570\u7684\u8fd4\u56de\u7c7b\u578b\u653e\u5728\u4e86\u5f62\u53c2\u5217\u8868\u4e4b\u540e\uff0c\u5c31\u53ef\u4ee5\u5f88\u6e05\u695a\u5730\u770b\u5230func\u51fd\u6570\u8fd4\u56de\u7684\u662f\u4e00\u4e2a\u6307\u9488\uff0c\u5e76\u4e14\u8be5\u6307\u9488\u6307\u5411\u4e86\u542b\u670910\u4e2a\u6574\u6570\u7684\u6570\u7ec4\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>auto func(int i) -> int(*)&#91;10]<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Lambda\u51fd\u6570\u6307\u5b9a\u8fd4\u56de\u7c7b\u578b<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>auto lambdafunc = &#91;](int a,int b) -> double\n    {\n        return static_cast&lt;double>(a) \/ b;\n    };<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u63a8\u5bfc\u51fd\u6570\u6a21\u677f\u7c7b\u578b<\/h3>\n\n\n\n<p>\u9700\u8981\u7528\u5230decltype\u8bf4\u660e\u7b26<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream>\n\nusing namespace std;\n\ntemplate&lt;class T1, class T2>\nauto sum(T1 t1, T2 t2) -> decltype(t1 + t2)\n{\n    return t1 + t2;\n}\n\nint main()\n{\n    auto s1 = sum(2, 3);\n    cout &lt;&lt; \"sum(2, 3)=\" &lt;&lt; s1 &lt;&lt; endl;\n    cout &lt;&lt; \"s1 type: \" &lt;&lt; typeid(s1).name() &lt;&lt; endl;\n\n    auto s2 = sum(2.0, 3.0);\n    cout &lt;&lt; \"sum(2.0, 3.0)=\" &lt;&lt; s2 &lt;&lt; endl;\n    cout &lt;&lt; \"s2 type: \" &lt;&lt; typeid(s2).name() &lt;&lt; endl;\n\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>\u5728C++11\u53ca\u4ee5\u540e\u7684\u6807\u51c6\u4e2d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u4e0d\u5e26\u5c3e\u7f6e\u8fd4\u56de\u7c7b\u578b\u7684<code>auto<\/code>\u6765\u7f16\u5199\u51fd\u6570\u6a21\u677f\uff0c\u7f16\u8bd1\u5668\u4f1a\u6839\u636e<code>return<\/code>\u8bed\u53e5\u4e2d\u7684\u8868\u8fbe\u5f0f\u6765\u81ea\u52a8\u63a8\u65ad\u8fd4\u56de\u7c7b\u578b\u3002\u56e0\u6b64\uff0c\u4f60\u7684\u793a\u4f8b\u4ee3\u7801\u662f\u5408\u6cd5\u7684\uff0c\u5e76\u4e14\u7f16\u8bd1\u5668\u4f1a\u6839\u636e<code>x<\/code>\u548c<code>y<\/code>\u7684\u7c7b\u578b\u63a8\u65ad\u51fa\u8fd4\u56de\u7c7b\u578b\u3002<\/p>\n\n\n\n<p>\u6240\u4ee5\uff0c\u8fd9\u4e2a\u4ee3\u7801\u4e0d\u9700\u8981\u5c3e\u7f6e\u8fd4\u56de\u7c7b\u578b\uff0c\u4ee5\u4e0b\u4ee3\u7801\u662f\u5408\u6cd5\u7684\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>template &lt;typename T, typename U&gt;\nauto multiply(T x, U y) {\n    return x * y;\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>C++11 \u6807\u51c6\u4e2d\u5f15\u5165\u4e86\u5c3e\u7f6e\u8fd4\u56de\u7c7b\u578b \u5f53\u8fd4\u56de\u7c7b\u578b\u6bd4\u8f83\u590d\u6742\u7684\u65f6\u5019\uff0c\u8fd4\u56de\u7c7b\u578b\u540e\u7f6e\u53ef\u80fd\u4f1a\u662f\u4e00\u4e2a\u4e0d\u9519\u7684\u9009\u62e9\u3002\u4efb\u4f55\u51fd\u6570\u7684\u5b9a\u4e49\u90fd\u80fd\u4f7f\u7528\u5c3e\u7f6e\u8fd4\u56de &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"emotion":"","emotion_color":"","title_style":"","license":"","footnotes":""},"categories":[22],"tags":[],"class_list":["post-2261","post","type-post","status-publish","format-standard","hentry","category-c"],"_links":{"self":[{"href":"https:\/\/courtship.top\/index.php\/wp-json\/wp\/v2\/posts\/2261","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/courtship.top\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/courtship.top\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/courtship.top\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/courtship.top\/index.php\/wp-json\/wp\/v2\/comments?post=2261"}],"version-history":[{"count":0,"href":"https:\/\/courtship.top\/index.php\/wp-json\/wp\/v2\/posts\/2261\/revisions"}],"wp:attachment":[{"href":"https:\/\/courtship.top\/index.php\/wp-json\/wp\/v2\/media?parent=2261"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/courtship.top\/index.php\/wp-json\/wp\/v2\/categories?post=2261"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/courtship.top\/index.php\/wp-json\/wp\/v2\/tags?post=2261"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}