Range for cycle equivalent

So, according to n2243 , a range-based loop is equivalent to this:

{
     auto && __range = ( expression );

     for ( auto __begin = std::Range<_RangeT>::begin(__range),
                  __end = std::Range<_RangeT>::end(__range);
          __begin != __end;
          ++__begin )
     {
         for-range-declaration = *__begin;
         statement
     }    
}

Then he says 2 If the header <iterator_concept> is not included prior to a use of the range-based for statement, the program is ill-formed., so I ask how relevant this is. I am also curious about what std::Rangeis, or is it just an implementation detail. The closest I can find is the n3350 .

This answer builds on this information and says:

The range for operation is as fast as possible, since it caches the end of the iterator [quote], uses pre-increment and only plays the iterator once.

therefore, if you tend to write:

for(iterator i = cont.begin(); i != cont.end(); i++) { /**/ }

Then yes, the range can be a little faster, since it also write there, there is no reason not to use it (if necessary).

P.S. , . , .

, . , . , , auto it = s.rbegin(); it != s.rend(); ++it, , , , , begin end. , , , , begin end? , , , - 2007 .

+4
1

@DyP, . ++ 11 6.5.4 for [stmt.ranged]:

1 for

  for ( for-range-declaration : expression ) statement

range-init ,

  ( expression )

for

  for ( for-range-declaration : braced-init-list ) statement

range-init init-init. for

  {
    auto && __range = range-init;
    for ( auto __begin = begin-expr,
               __end = end-expr;
          __begin != __end;
          ++__begin ) {
      for-range-declaration = *__begin;
      statement
    }
  }

__range, __begin __end - , , _RangeT - , begin-expr end-expr :

  • if _RangeT - , begin-expr end-expr - __range __range + __bound, , __bound - . _RangeT - , ;

  • _RangeT , unqualified-ids begin end _RangeT, (3.4.5), ( ) , begin-expr end-expr - __range.begin() __range.end() ;

  • begin-expr end-expr begin(__range) end(__range) , begin end (3.4.2). , namespace std - .

[:

  int array[5] = { 1, 2, 3, 4, 5 };
  for (int& x : array)
    x *= 2;

-end ]

2 --seq - , constexpr.

+3

All Articles