<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto"><div><br></div><div><br>On Jan 11, 2017, at 11:15 AM, JF Bastien &lt;<a href="mailto:jfb@chromium.org">jfb@chromium.org</a>&gt; wrote:<br><br></div><blockquote type="cite"><div><div dir="ltr">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><br></div><div><br></div><div>e.g. I think this is great:</div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div><font face="monospace, monospace">auto ptr = std::make_unique&lt;Foo&gt;(bar);</font></div></blockquote><div>Proposed rule: if the type is obvious because it's on the line, then auto is good.</div><div>Similarly:</div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div><font face="monospace, monospace">auto i = static_cast&lt;int&gt;(j);</font></div></blockquote><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div><font face="monospace, monospace">auto foo = make_foo();</font></div></blockquote><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div><font face="monospace, monospace">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>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><br></div><div>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><div><div><br></div><div>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><div><br></div><div>- Saam</div><div><br></div><div><blockquote type="cite"><div><div dir="ltr"><div><br></div><div><br></div><div>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"><div><font face="monospace, monospace">for (const auto&amp; v : cont) { /* don't change v */ }</font></div><div><font face="monospace, monospace">for auto&amp; v : cont) { /* change v */ }</font></div></blockquote><div>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">Here's more discussion on this</a>, including a recommendation to use <font face="monospace, monospace">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><br></div><div><br></div><div>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"><div><font face="monospace, monospace">struct Foo {</font></div><div><font face="monospace, monospace">&nbsp; typedef Something Bar;</font></div><div><font face="monospace, monospace">&nbsp; // ...</font></div><div><font face="monospace, monospace">&nbsp; Bar doIt();</font></div><div><font face="monospace, monospace">};</font></div><div><font face="monospace, monospace">auto Foo::doIt() -&gt; Bar</font></div><div><font face="monospace, monospace">{</font></div><div><font face="monospace, monospace">&nbsp; // ...</font></div><div><font face="monospace, monospace">}</font></div></blockquote><div>Why? Because <font face="monospace, monospace">Bar</font> is scoped to <font face="monospace, monospace">Foo</font>! It looks odd the first time, but I think this is idiomatic "modern" C++.</div><div><br></div><div><br></div><div>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"><div><font face="monospace, monospace">auto ohMy()</font></div><div><font face="monospace, monospace">{</font></div><div><font face="monospace, monospace">&nbsp; struct { int a; float b; } result;</font></div><div><font face="monospace, monospace">&nbsp; // ...</font></div><div><font face="monospace, monospace">&nbsp; return result;</font></div><div><font face="monospace, monospace">}</font></div><div><font face="monospace, monospace">void yeah()</font></div><div><font face="monospace, monospace">{</font></div><div><font face="monospace, monospace">&nbsp; auto myMy = ohMy();</font></div><div><font face="monospace, monospace">&nbsp; dataLogLn(myMy.a, myMy.b);</font></div><div><font face="monospace, monospace">}</font></div></blockquote><div>I initially had that with <a href="https://github.com/WebKit/webkit/blob/master/Source/WTF/wtf/Atomics.h#L385">consumeLoad</a>, which returns a <font face="monospace, monospace">T</font> as well as a <font face="monospace, monospace">ConsumeDependency</font>. I couldn't care less about the container for&nbsp;<font face="monospace, monospace">T</font>&nbsp;and&nbsp;<font face="monospace, monospace">ConsumeDependency</font>, I just want these two values.</div></div>
</div></blockquote><blockquote type="cite"><div><span>_______________________________________________</span><br><span>webkit-dev mailing list</span><br><span><a href="mailto:webkit-dev@lists.webkit.org">webkit-dev@lists.webkit.org</a></span><br><span><a href="https://lists.webkit.org/mailman/listinfo/webkit-dev">https://lists.webkit.org/mailman/listinfo/webkit-dev</a></span><br></div></blockquote></div></div></div></div></body></html>