<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">My take-away from this discussion so far is that there is actually very little consensus on usage of auto, which means there’s probably very little room for actual style guideline rules.<div class=""><br class=""></div><div class="">I think there are two very limited rules that are probably not objectionable to anybody.</div><div class=""><br class=""></div><div class="">1 - If you are using auto for a raw pointer type, you should use auto*</div><div class="">2 - If you are using auto in a range-based for loop for values that aren’t pointers, you should use (const) auto&amp;</div><div class=""><br class=""></div><div class="">If there’s no objections to these rules, I think it’s valuable to have them in the style guidelines at the very least.</div><div class=""><br class=""></div><div class="">Thanks,</div><div class="">~Brady</div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><div class=""><div><blockquote type="cite" class=""><div class="">On Jan 11, 2017, at 10:27 PM, saam barati &lt;<a href="mailto:saambarati1@gmail.com" class="">saambarati1@gmail.com</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="content-type" content="text/html; charset=utf-8" class=""><div dir="auto" class=""><div class=""><br class=""></div><div class=""><br class="">On Jan 11, 2017, at 11:15 AM, JF Bastien &lt;<a href="mailto:jfb@chromium.org" class="">jfb@chromium.org</a>&gt; wrote:<br class=""><br class=""></div><blockquote type="cite" class=""><div class=""><div dir="ltr" class="">Would it be helpful to focus on small well-defined cases where auto makes sense, and progressively grow that list as we see fit?<div class=""><br class=""></div><div class=""><br class=""></div><div class="">e.g. I think this is great:</div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px" class=""><div class=""><font face="monospace, monospace" class="">auto ptr = std::make_unique&lt;Foo&gt;(bar);</font></div></blockquote><div class="">Proposed rule: if the type is obvious because it's on the line, then auto is good.</div><div class="">Similarly:</div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px" class=""><div class=""><font face="monospace, monospace" class="">auto i = static_cast&lt;int&gt;(j);</font></div></blockquote><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px" class=""><div class=""><font face="monospace, monospace" class="">auto foo = make_foo();</font></div></blockquote><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px" class=""><div class=""><font face="monospace, monospace" class="">auto bar = something.get_bar(); // Sometimes, "bar" is obvious.</font></div></blockquote></div></div></blockquote>I'm not sure I agree with this style. There are times where the type of an auto variable is obvious-enough, but it's almost&nbsp;<i class="">never</i>&nbsp;more obvious than actually writing out the types. Writing out types, for my brain at least, almost always makes the code easier to understand. The most obvious place where I prefer auto over explicit types is when something has a lot of template bloat.<div class=""><br class=""></div><div class="">I feel like the places where auto makes the code better are limited, but places where auto makes the code more confusing, or requires me to spend more time figuring it out, are widespread. (Again, this is how my brain reads code.)<br class=""><div class=""><div class=""><br class=""></div><div class="">Also, I completely agree with Geoff that I use types to grep around the source code and to figure out what data structures are being used. If we used auto more inside JSC it would hurt my workflow for reading and understanding new code.<br class=""><div class=""><br class=""></div><div class="">- Saam</div><div class=""><br class=""></div><div class=""><blockquote type="cite" class=""><div class=""><div dir="ltr" class=""><div class=""><br class=""></div><div class=""><br class=""></div><div class="">Range-based loops are a bit tricky. IMO containers with "simple" types are good candidates for either:</div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px" class=""><div class=""><font face="monospace, monospace" class="">for (const auto&amp; v : cont) { /* don't change v */ }</font></div><div class=""><font face="monospace, monospace" class="">for auto&amp; v : cont) { /* change v */ }</font></div></blockquote><div class="">But what's "simple"? I'd say all numeric, pointer, and string types at least. It gets tricky for more complex types, and I'd often rather have the type in the loop. <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n3853.htm" class="">Here's more discussion on this</a>, including a recommendation to use <font face="monospace, monospace" class="">auto&amp;&amp;</font> on range-based loops! I think this gets confusing, and I'm not a huge fan of r-value references everywhere.</div><div class=""><br class=""></div><div class=""><br class=""></div><div class="">Here's another I like, which Yusuke pointed out a while ago (in ES6 Module's implementation?):</div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px" class=""><div class=""><font face="monospace, monospace" class="">struct Foo {</font></div><div class=""><font face="monospace, monospace" class="">&nbsp; typedef Something Bar;</font></div><div class=""><font face="monospace, monospace" class="">&nbsp; // ...</font></div><div class=""><font face="monospace, monospace" class="">&nbsp; Bar doIt();</font></div><div class=""><font face="monospace, monospace" class="">};</font></div><div class=""><font face="monospace, monospace" class="">auto Foo::doIt() -&gt; Bar</font></div><div class=""><font face="monospace, monospace" class="">{</font></div><div class=""><font face="monospace, monospace" class="">&nbsp; // ...</font></div><div class=""><font face="monospace, monospace" class="">}</font></div></blockquote><div class="">Why? Because <font face="monospace, monospace" class="">Bar</font> is scoped to <font face="monospace, monospace" class="">Foo</font>! It looks odd the first time, but I think this is idiomatic "modern" C++.</div><div class=""><br class=""></div><div class=""><br class=""></div><div class="">I also like creating unnamed types, though I know this isn't everyone's liking:</div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px" class=""><div class=""><font face="monospace, monospace" class="">auto ohMy()</font></div><div class=""><font face="monospace, monospace" class="">{</font></div><div class=""><font face="monospace, monospace" class="">&nbsp; struct { int a; float b; } result;</font></div><div class=""><font face="monospace, monospace" class="">&nbsp; // ...</font></div><div class=""><font face="monospace, monospace" class="">&nbsp; return result;</font></div><div class=""><font face="monospace, monospace" class="">}</font></div><div class=""><font face="monospace, monospace" class="">void yeah()</font></div><div class=""><font face="monospace, monospace" class="">{</font></div><div class=""><font face="monospace, monospace" class="">&nbsp; auto myMy = ohMy();</font></div><div class=""><font face="monospace, monospace" class="">&nbsp; dataLogLn(myMy.a, myMy.b);</font></div><div class=""><font face="monospace, monospace" class="">}</font></div></blockquote><div class="">I initially had that with <a href="https://github.com/WebKit/webkit/blob/master/Source/WTF/wtf/Atomics.h#L385" class="">consumeLoad</a>, which returns a <font face="monospace, monospace" class="">T</font> as well as a <font face="monospace, monospace" class="">ConsumeDependency</font>. I couldn't care less about the container for&nbsp;<font face="monospace, monospace" class="">T</font>&nbsp;and&nbsp;<font face="monospace, monospace" class="">ConsumeDependency</font>, I just want these two values.</div></div>
</div></blockquote><blockquote type="cite" class=""><div class=""><span class="">_______________________________________________</span><br class=""><span class="">webkit-dev mailing list</span><br class=""><span class=""><a href="mailto:webkit-dev@lists.webkit.org" class="">webkit-dev@lists.webkit.org</a></span><br class=""><span class=""><a href="https://lists.webkit.org/mailman/listinfo/webkit-dev" class="">https://lists.webkit.org/mailman/listinfo/webkit-dev</a></span><br class=""></div></blockquote></div></div></div></div></div>_______________________________________________<br class="">webkit-dev mailing list<br class=""><a href="mailto:webkit-dev@lists.webkit.org" class="">webkit-dev@lists.webkit.org</a><br class="">https://lists.webkit.org/mailman/listinfo/webkit-dev<br class=""></div></blockquote></div><br class=""></div></div></body></html>