[llvm-dev] When to use auto instead of iterator/const_iterator?

陳韋任 via llvm-dev llvm-dev at lists.llvm.org
Tue May 2 06:36:58 PDT 2017


Hi All,

  While reading LLVM source code, sometimes I am wondering when should we
use auto instead of iterator/const_iterator.
I want to use the patch [1] I sent before as an example. Could someone give
me advice/guideline here? Also, I have another
question. Sometimes the for-loop uses const_iterator, say

  for (SUnit::const_succ_iterator I = SU->Succs.begin(), E =
SU->Succs.end();

         I != E; ++I)

    if (I->isCtrl())

      NumberDeps++;

Can we rewrite above code as,

  for (auto &Succ : SU->Succs)

    if (Succ.isCtrl())

      NumberDeps++;
Or do we need to use `const auto &` instead, like this

  for (
​const ​
auto &Succ : SU->Succs)

    if (Succ.isCtrl())

      NumberDeps++;


Thanks.


[1] https://reviews.llvm.org/D32682

Regards,
chenwj

-- 
Wei-Ren Chen (陳韋任)
Homepage: https://people.cs.nctu.edu.tw/~chenwj
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20170502/89ac5d06/attachment.html>


More information about the llvm-dev mailing list