<div dir="ltr"><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></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 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><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><p class="gmail-p1">









</p><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)<p></p><p class="gmail-p1" style="font-family:arial,helvetica,sans-serif"><span class="gmail-s1">    if (Succ.isCtrl())</span></p><p class="gmail-p1" style="font-family:arial,helvetica,sans-serif"></p><div class="gmail_default" style="font-family:arial,helvetica,sans-serif">      NumberDeps++; </div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif"><br></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif"><br></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif">Thanks.</div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif"> </div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif"><br></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif">[1] <a href="https://reviews.llvm.org/D32682">https://reviews.llvm.org/D32682</a></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif"><br></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif">Regards,</div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif">chenwj</div><div><br></div>-- <br><div class="gmail_signature"><div dir="ltr"><div>Wei-Ren Chen (陳韋任)<br>Homepage: <a href="https://people.cs.nctu.edu.tw/~chenwj" target="_blank">https://people.cs.nctu.edu.tw/~chenwj</a></div></div></div>
</div>