<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=""><div class=""><br class=""></div><div class="">As for:</div><div class="">- range based for: Use them wherever possible instead of the iterators. There are some cases however where you cannot avoid the iterators (for example if you want to call `iter = container.erase(iter)` in a loop)</div><div class="">- Avoid `auto` unless the type is obviously clear from the context:</div><br class=""><div><blockquote type="cite" class=""><div class="">On May 2, 2017, at 6:36 AM, 陳韋任 via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org" class="">llvm-dev@lists.llvm.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class=""><div class="gmail_default" style="font-family:arial,helvetica,sans-serif">Hi All,</div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif"><br class=""></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif">  While reading LLVM source code, sometimes I am wondering when should we use auto instead of iterator/const_iterator.</div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif">I want to use the patch [1] I sent before as an example. Could someone give me advice/guideline here? Also, I have another</div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif">question. Sometimes the for-loop uses const_iterator, say</div></div></div></blockquote><div><div class="">These are topics discussed in the developer policy: <a href="http://llvm.org/docs/DeveloperPolicy.html" class="">http://llvm.org/docs/DeveloperPolicy.html</a></div><div class=""><br class=""></div><div class="">While reading LLVM sourcecode keep in mind that sometimes the sourcecode is older than some rules in the policy and might violate them. If you write new code however it should adhere to the policy.</div></div><br class=""><blockquote type="cite" class=""><div dir="ltr" class=""><div class="gmail_default" style="font-family:arial,helvetica,sans-serif"><p class="gmail-p1"><span class="gmail-s1">  for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();</span></p><p class="gmail-p1">         I != E; ++I)</p><p class="gmail-p1"><span class="gmail-s1">    if (I->isCtrl())</span></p><p class="gmail-p1"><span class="gmail-s1">      NumberDeps++;</span></p><div class=""><br class=""></div></div></div></blockquote>This should work just as well with a range based for, so you should prefer that over the iterator!<br class=""><blockquote type="cite" class=""><div class=""><div dir="ltr" class=""><div class="gmail_default" style="font-family:arial,helvetica,sans-serif"><p class="gmail-p1"><span class="gmail-s1">Can we rewrite above code as,</span></p><p class="gmail-p1"><span class="gmail-s1">  for (auto &Succ : SU->Succs)</span></p><p class="gmail-p1"><span class="gmail-s1">    if (Succ.isCtrl())</span></p><div class="">









<br class="webkit-block-placeholder"></div><p class="gmail-p1"><span class="gmail-s1">      NumberDeps++;</span></p></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif">Or do we need to use `const auto &` instead, like this</div><p class="gmail-p1" style="font-family:arial,helvetica,sans-serif"><span class="gmail-s1">  for (</span></p><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;display:inline">​const ​</div>auto &Succ : SU->Succs)<div class=""><br class="webkit-block-placeholder"></div><p class="gmail-p1" style="font-family:arial,helvetica,sans-serif"><span class="gmail-s1">    if (Succ.isCtrl())</span></p><div style="font-family: arial, helvetica, sans-serif;" class=""><br class="webkit-block-placeholder"></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif">      NumberDeps++; </div></div></div></blockquote><div><br class=""></div><div>This should be written as:</div><div><br class=""></div><div>for (const SDep &Succ : SU->Succs) {</div><div>  if (Succ.isCtrl())</div><div>    ++NUmberDeps;</div><div>}</div><div><br class=""></div><div>And I think the LLVM community never agreed whether you need {} around the for in a case like this. Some add the {} here, some people don't. Attempts to standardize this as in <a href="https://reviews.llvm.org/D26943" class="">https://reviews.llvm.org/D26943</a> did not reach agreement.</div><div><br class=""></div><div>- Matthias</div></div></body></html>