A range-based for
statement is defined in §6.5.4 to be equivalent to:
{
auto && __range = range-init;
for ( auto __begin = begin-expr,
__end = end-expr;
__begin != __end;
++__begin ) {
for-range-declaration = *__begin;
statement
}
}
where range-init
is defined for the two forms of range-based for
as:
for ( for-range-declaration : expression ) => ( expression )
for ( for-range-declaration : braced-init-list ) => braced-init-list
(the clause further specifies the meaning of the other sub-expressions)
Why is __range
given the deduced type auto&&
? My understanding of auto&&
is that it's useful for preserving the original valueness (lvalue/rvalue) of an expression by passing it through std::forward
. However, __range
isn't passed anywhere through std::forward
. It's only used when getting the range iterators, as one of __range
, __range.begin()
, or begin(__range)
.
What's the benefit here of using the "universal reference" auto&&
? Wouldn't auto&
suffice?
Note: As far as I can tell, the proposal doesn't say anything about the choice of auto&&
.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…