{"id":86,"date":"2018-12-25T02:49:23","date_gmt":"2018-12-25T05:49:23","guid":{"rendered":"http:\/\/35.243.195.209\/?p=86"},"modified":"2019-01-25T23:18:13","modified_gmt":"2019-01-26T07:18:13","slug":"auto-type","status":"publish","type":"post","link":"https:\/\/nanzhou.cc\/index.php\/2018\/12\/25\/auto-type\/","title":{"rendered":"Auto Type"},"content":{"rendered":"<h2>Summary<\/h2>\n<p>In this post, I will introduce <code>auto<\/code>, <code>const auto<\/code>, <code>auto&amp;<\/code>, <code>const auto&amp;<\/code>, <code>auto&amp;&amp;<\/code>, and <code>const auto&amp;&amp;<\/code>.<\/p>\n<h2>Conclusion<\/h2>\n<p>We provide the conclusion first.<\/p>\n<ol>\n<li>When you want a <strong>read-only<\/strong> access (generic or non-generic codes), use <code>auto const &amp;<\/code>, which will give you a reference rather than a copy. <\/li>\n<li>When you want a <strong>read-write<\/strong> access in <strong>generic<\/strong> codes, use <code>auto &amp;&amp;<\/code>.<\/li>\n<li>When you want a <strong>read-write<\/strong> access in <strong>non-generic<\/strong> codes, use <code>auto &amp;<\/code>.<\/li>\n<\/ol>\n<h2>Details<\/h2>\n<h3><code>auto<\/code><\/h3>\n<p>Auto will create a copy of each element in the range so use this variant when you want to work with a copy.<\/p>\n<p>Note the following situations.<br \/>\n1. Containers return proxy objects upon dereferencing of their iterators.<\/p>\n<pre><code class=\"c\">std::vector&lt;bool&gt; vec{false, false, false};\nfor (auto x : vec) {\n    x = true; \/\/ now vec is {true, true, true}\n}\n<\/code><\/pre>\n<h3><code>const auto<\/code><\/h3>\n<p>It will provide an immutable copy of each element.<br \/>\nHowever we should avoid using <code>const auto<\/code> because there is no reason we create a copy. Thus, use <code>const auto &amp;<\/code> instead.<\/p>\n<h3><code>const auto&amp;<\/code> or <code>auto const &amp;<\/code><\/h3>\n<p>This is the <strong>number one choice<\/strong> for iterating over a range when all you want to is read its elements. No copies are made and the compiler can verify that you indeed do not modify the elements.<\/p>\n<h3><code>auto&amp;<\/code><\/h3>\n<p>Use <code>auto&amp;<\/code> when you want to modify elements in the range in non-generic codes. <code>&amp;<\/code> means it will make references to the original elements in the range. See special situations (eg, inside templates) in <a href=\"https:\/\/blog.petrzemek.net\/2016\/08\/17\/auto-type-deduction-in-range-based-for-loops\/\">reference<\/a>.<\/p>\n<h3><code>auto&amp;&amp;<\/code><\/h3>\n<p>Use <code>auto&amp;&amp;<\/code> when you want to modify elements in the range in generic codes. Actually it also works in non-generic codes, but it unnecessarily confuses people. Details are in <a href=\"https:\/\/blog.petrzemek.net\/2016\/08\/17\/auto-type-deduction-in-range-based-for-loops\/\">reference<\/a>.<\/p>\n<h3><code>const auto&amp;&amp;<\/code><\/h3>\n<p>There is no reason for choosing this variant over <code>const auto&amp;<\/code>.<\/p>\n<h2>Reference<\/h2>\n<p>See <a href=\"https:\/\/blog.petrzemek.net\/2016\/08\/17\/auto-type-deduction-in-range-based-for-loops\/\">this blog<\/a> for more details and some special situations.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary In this post, I will introduce auto, const auto, auto&amp;, const auto&amp;, auto&amp;&amp;, and const auto&amp;&amp;. Conclusion We provide the conclusion first. When you want a read-only access (generic or non-generic codes), use auto const &amp;, which will give you a reference rather than a copy. When you want a read-write access in generic&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,5],"tags":[],"class_list":["post-86","post","type-post","status-publish","format-standard","hentry","category-c","category-proglang"],"_links":{"self":[{"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/posts\/86","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/comments?post=86"}],"version-history":[{"count":5,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/posts\/86\/revisions"}],"predecessor-version":[{"id":227,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/posts\/86\/revisions\/227"}],"wp:attachment":[{"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/media?parent=86"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/categories?post=86"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/tags?post=86"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}