C++11 range based loop: How does it really work

前端 未结 3 1781
梦毁少年i
梦毁少年i 2020-12-03 22:02

I know how this loop works, and how I can use it in practical problems. But I want to know what is happening under the hood. I thought that this loop was similar to a regula

3条回答
  •  旧时难觅i
    2020-12-03 22:33

    For unterstanding purpose you can think of it as if the compiler is replacing for (auto x: y) {...} with for (auto i = begin(y), end = end(y); i != end; ++i) { auto x = *i; {...} }.

    For std::vector begin(y)/end(y) will resolve (via the adl) to std::begin(y)/std::end(y) versions that would call y.begin()/y.end() respectively.

提交回复
热议问题