<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40"><head><meta http-equiv=Content-Type content="text/html; charset=utf-8"><meta name=Generator content="Microsoft Word 15 (filtered medium)"><style><!--
/* Font Definitions */
@font-face
{font-family:"Cambria Math";
panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
{font-family:Calibri;
panose-1:2 15 5 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0in;
margin-bottom:.0001pt;
font-size:11.0pt;
font-family:"Calibri",sans-serif;}
a:link, span.MsoHyperlink
{mso-style-priority:99;
color:blue;
text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
{mso-style-priority:99;
color:purple;
text-decoration:underline;}
p.msonormal0, li.msonormal0, div.msonormal0
{mso-style-name:msonormal;
mso-margin-top-alt:auto;
margin-right:0in;
mso-margin-bottom-alt:auto;
margin-left:0in;
font-size:11.0pt;
font-family:"Calibri",sans-serif;}
span.EmailStyle18
{mso-style-type:personal-reply;
font-family:"Calibri",sans-serif;}
.MsoChpDefault
{mso-style-type:export-only;
font-family:"Calibri",sans-serif;}
@page WordSection1
{size:8.5in 11.0in;
margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
{page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]--></head><body lang=EN-US link=blue vlink=purple><div class=WordSection1><p class=MsoNormal>> > Perhaps the rule came be rewritten more generally, as in “auto should generally only be<o:p></o:p></p><p class=MsoNormal>> > used when the inferred type is obvious from context,<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>> I still think this is the correct core rule: there needs to be *some* reason why the type is<o:p></o:p></p><p class=MsoNormal>> contextually obvious. And honestly, I'm very happy erring on the side of writing out the<o:p></o:p></p><p class=MsoNormal>> type name. I think people are too eager to use `auto` because it is easy to write but it<o:p></o:p></p><p class=MsoNormal>> makes the types substantially harder for the reader to understand.<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>FWIW, auto != infer. Since AAA* was mentioned and I wrote that, I just wanted to clarify never intended it to mean to always use auto for type inference.<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>I’ve always tried to carefully say there are two forms, but people seem to fixate on the first one:<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal> auto x = non-cast-expr; // inferred type<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal> auto x = type( expr ); // explicit type – includes dynamic_cast<>, {} vs () syntax, etc.<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>The advice to “always auto” includes the second form, and so you can always use auto without loss of readability because it’s not in tension with stating an explicit type. FAQ: Why write the second form “auto x = type( expr );” instead of just the traditional “type x( expr );”? For several reasons, perhaps the most important being that with “auto” you can never forget to initialize a variable because the = is mandatory. There are also other advantages, such as no vexing parse, consistency with other left-to-right modern declarations such as using, and others; all these advantages apply to both forms above, you get them just by starting with “auto.”<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>But again this is just a “FWIW,” I’m just clarifying, not trying to campaign. The main thing for any team is to adopt a style that people are happy to live with.<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>Herb<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>* Actually AA these days. Thanks, Richard, both for promoting guaranteed copy elision and for pointing out that it eliminates the first A! Auto style is now easy and efficient to use with all types, even std::array and lock_guard. Insert gratuitous battery size joke here about how AA is an upgrade, carries more charge, etc.<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal><o:p> </o:p></p><div style='border:none;border-left:solid blue 1.5pt;padding:0in 0in 0in 4.0pt'><div><div style='border:none;border-top:solid #E1E1E1 1.0pt;padding:3.0pt 0in 0in 0in'><p class=MsoNormal><b>From:</b> cfe-dev <cfe-dev-bounces@lists.llvm.org> <b>On Behalf Of </b>Chandler Carruth via cfe-dev<br><b>Sent:</b> Tuesday, December 4, 2018 5:02 AM<br><b>To:</b> Chris Lattner <clattner@nondot.org><br><b>Cc:</b> llvm-dev@lists.llvm.org; cfe-dev@lists.llvm.org; Stephen Kelly <steveire@gmail.com><br><b>Subject:</b> Re: [cfe-dev] [llvm-dev] RFC: Modernizing our use of auto<o:p></o:p></p></div></div><p class=MsoNormal><o:p> </o:p></p><div><div><div><p class=MsoNormal>On Wed, Nov 28, 2018 at 6:25 PM Chris Lattner via cfe-dev <<a href="mailto:cfe-dev@lists.llvm.org">cfe-dev@lists.llvm.org</a>> wrote:<o:p></o:p></p></div><blockquote style='border:none;border-left:solid #CCCCCC 1.0pt;padding:0in 0in 0in 6.0pt;margin-left:4.8pt;margin-right:0in'><p class=MsoNormal>Generally no IMO, because the cases that produce optional are not obvious.<o:p></o:p></p></blockquote><div><p class=MsoNormal><o:p> </o:p></p></div><div><p class=MsoNormal>Just to say, +1 from me too.<o:p></o:p></p></div><div><p class=MsoNormal> <o:p></o:p></p></div><blockquote style='border:none;border-left:solid #CCCCCC 1.0pt;padding:0in 0in 0in 6.0pt;margin-left:4.8pt;margin-right:0in'><p class=MsoNormal><br>> * Can we use auto in c++14 lambda arguments with llvm::find_if(C, [](const auto& i) { ... }) for example?<br>> * We need to use auto for structured bindings, if those are used.<br><br>These both get into “yes, if the type is contextually obvious”. I’m not sure how to specify this though.<br><br>Perhaps the rule came be rewritten more generally, as in “auto should generally only be used when the inferred type is obvious from context, e.g. as a result of a cast like dyn_cast or as a result of constructing a value with a constructor containing a type name.”?<o:p></o:p></p></blockquote><div><p class=MsoNormal><o:p> </o:p></p></div><div><p class=MsoNormal>Strongly agree.<o:p></o:p></p></div><div><p class=MsoNormal><o:p> </o:p></p></div><div><p class=MsoNormal>I understand it may not be as precise and unambiguous as people would like, I still think this is the correct core rule: there needs to be *some* reason why the type is contextually obvious.<o:p></o:p></p></div><div><p class=MsoNormal><o:p> </o:p></p></div><div><p class=MsoNormal>And honestly, I'm very happy erring on the side of writing out the type name. I think people are too eager to use `auto` because it is easy to write but it makes the types substantially harder for the reader to understand.<o:p></o:p></p></div><div><p class=MsoNormal><o:p> </o:p></p></div><blockquote style='border:none;border-left:solid #CCCCCC 1.0pt;padding:0in 0in 0in 6.0pt;margin-left:4.8pt;margin-right:0in'><p class=MsoNormal>> In the case that came up in review for me, the code I submitted is<br>> <br>> template <typename BaseT, typename DerivedT><br>> void registerIfNodeMatcher(...) {<br>> auto NodeKind = ASTNodeKind::getFromNodeKind<DerivedT>();<br>> if (!NodeKind.isNone())<br>> NodeCtors[NodeKind] = std::make_pair(MatcherName, Descriptor);<br>> }<br>> <br>> but it was rejected as unreadable and I was given a link to the coding guidelines.<br><br>I agree, this seems overly strict.<o:p></o:p></p></blockquote><div><p class=MsoNormal><o:p> </o:p></p></div><div><p class=MsoNormal>I mean maybe. But looking at the example, I don't have a clue what type `NodeKind` is. I think this is borderline, and I think its fine to be somewhat subjective based on the reviewer that is more familiar with the code and APIs in question.<o:p></o:p></p></div><div><p class=MsoNormal> <o:p></o:p></p></div><blockquote style='border:none;border-left:solid #CCCCCC 1.0pt;padding:0in 0in 0in 6.0pt;margin-left:4.8pt;margin-right:0in'><p class=MsoNormal>> I'd also like to update them so that<br>> <br>> llvm::Optional<std::pair<std::string, MatcherCtor>><br>> getNodeConstructorType(ASTNodeKind targetType) {<br>> auto const &ctors = RegistryData->nodeConstructors();<br>> auto it = llvm::find_if(<br>> ctors, [targetType](const NodeConstructorMap::value_type &ctor) {<br>> return ctor.first.isSame(targetType);<br>> });<br>> if (it == ctors.end())<br>> return llvm::None;<br>> return it->second;<br>> }<br>> <br>> is acceptable. The `auto it` is already acceptable, but the `auto const& ctors` depends on an interpretation of the guideline and was rejected in the review I submitted.<br><br>This seems like a step too far from me. I don’t know what nodeConstructors is here at all, and you’ve erased all type information about what you’re dealing with.<o:p></o:p></p></blockquote><div><p class=MsoNormal><o:p> </o:p></p></div><div><p class=MsoNormal>Strongly agree. The `auto it` continues to seem fine, but the `ctors` here seems super confusing to leave off all type information.<o:p></o:p></p></div></div></div></div></div></body></html>