How does the range-based for work for plain arrays?

后端 未结 6 1113
暗喜
暗喜 2020-11-27 11:19

In C++11 you can use a range-based for, which acts as the foreach of other languages. It works even with plain C arrays:

int number         


        
6条回答
  •  清歌不尽
    2020-11-27 12:07

    It knows when to stop because it knows the bounds of static arrays.

    I'm not sure what do you mean by "dynamic arrays", in any case, if not iterating over static arrays, informally, the compiler looks up the names begin and end in the scope of the class of the object you iterate over, or looks up for begin(range) and end(range) using argument-dependent lookup and uses them as iterators.

    For more information, in the C++11 standard (or public draft thereof), "6.5.4 The range-based for statement", pg.145

提交回复
热议问题