{"id":2392,"date":"2017-01-25T17:53:02","date_gmt":"2017-01-25T09:53:02","guid":{"rendered":"https:\/\/boweihe.me\/?p=2392"},"modified":"2017-01-25T17:53:02","modified_gmt":"2017-01-25T09:53:02","slug":"c%e6%a8%a1%e6%9d%bf%e5%85%83%e7%bc%96%e7%a8%8b-%e4%b9%a0%e9%a2%98-add_const_ref","status":"publish","type":"post","link":"https:\/\/dayandcarrot.space\/?p=2392","title":{"rendered":"C++\u6a21\u677f\u5143\u7f16\u7a0b \u4e60\u9898: add_const_ref"},"content":{"rendered":"<p>\u8981\u5b66\u7684\u4e1c\u897f\u8fd8\u6709\u5f88\u591a\u554a&#8230;<br \/>\n\u4e60\u98982-0. \u7f16\u5199\u4e00\u4e2a\u4e00\u5143\u5143\u51fd\u6570add_const_ref&lt;T&gt;\uff0c\u5982\u679cT\u662f\u4e00\u4e2a\u5f15\u7528\u7c7b\u578b\uff0c\u5c31\u8fd4\u56deT\uff0c\u5426\u5219\u8fd4\u56deT const&amp;.<\/p>\n<pre class=\"lang:c++ decode:true \" title=\"main.cpp\">#include \"add_const_ref.h\"\nint main()\n{\n\tint a = 5;\n\tadd_const_ref&lt;int&gt;::type c = a;\n\tadd_const_ref&lt;int&amp;&gt;::type d = a;\n\treturn 0;\n}<\/pre>\n<p>&nbsp;<\/p>\n<pre class=\"lang:c++ decode:true \" title=\"add_const_ref.h\">#ifndef ADD_CONST_REF\n#define ADD_CONST_REF\ntemplate &lt;class T&gt;\nstruct add_const_ref;\ntemplate&lt;class T&gt;\nstruct add_const_ref\n{\n\ttypedef T const&amp; type;\n};\ntemplate&lt;class T&gt;\nstruct add_const_ref&lt;T&amp;&gt;\n{\n\ttypedef T type;\n};\n#endif<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8981\u5b66\u7684\u4e1c\u897f\u8fd8\u6709\u5f88\u591a\u554a&#8230; \u4e60\u98982-0. \u7f16\u5199\u4e00\u4e2a\u4e00\u5143\u5143\u51fd\u6570add_const_ref&lt;T&gt;\uff0c\u5982\u679cT\u662f\u4e00\u4e2a\u5f15\u7528\u7c7b\u578b\uff0c\u5c31\u8fd4\u56deT\uff0c\u5426\u5219\u8fd4\u56deT const&amp;. #include &#8220;add_const_ref.h&#8221; int main() { int a = 5; add_const_ref&lt;int&gt;::type c = a; add_const_ref&lt;int&amp;&gt;::type d = a; return 0; } &nbsp; #ifndef ADD_CONST_REF #define ADD_CONST_REF template &lt;class T&gt; struct add_const_ref; template&lt;class T&gt; struct add_const_ref { typedef T const&amp; type; }; template&lt;class T&gt; struct add_const_ref&lt;T&amp;&gt; { typedef T type; }; #endif &nbsp;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[28,172],"class_list":["post-2392","post","type-post","status-publish","format-standard","hentry","category-study","tag-c","tag-172"],"_links":{"self":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/2392","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2392"}],"version-history":[{"count":0,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/2392\/revisions"}],"wp:attachment":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2392"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2392"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2392"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}