GCC documentation about auto-vectorization doesn't mention anything about the range-based for
loop. Also, its code boils down to:
{
auto && __range = range_expression ;
for (auto __begin = begin_expr,
__end = end_expr;
__begin != __end; ++__begin) {
range_declaration = *__begin;
loop_statement
}
}
So, technically speaking, any flag helping to auto-vectorize the constructs in this kind of regular for
should auto-vectorize a similar range-based for
loop. I really do this compilers only translate range-based for
loops to regular for
loops, then let the auto-vectorization do its job on these old loops. Which means that there is no need for a flag to tell your compiler to auto-vectorize your range-based for
loops in a any scenario.
Since GCC's implementation was asked for, here is the relevant comment in the source code describing what is actually done for the range-based for
loop (you can check the implementation file parser.c if you want to have a look at the code):
/* Converts a range-based for-statement into a normal
for-statement, as per the definition.
for (RANGE_DECL : RANGE_EXPR)
BLOCK
should be equivalent to:
{
auto &&__range = RANGE_EXPR;
for (auto __begin = BEGIN_EXPR, end = END_EXPR;
__begin != __end;
++__begin)
{
RANGE_DECL = *__begin;
BLOCK
}
}
If RANGE_EXPR is an array:
BEGIN_EXPR = __range
END_EXPR = __range + ARRAY_SIZE(__range)
Else if RANGE_EXPR has a member 'begin' or 'end':
BEGIN_EXPR = __range.begin()
END_EXPR = __range.end()
Else:
BEGIN_EXPR = begin(__range)
END_EXPR = end(__range);
If __range has a member 'begin' but not 'end', or vice versa, we must
still use the second alternative (it will surely fail, however).
When calling begin()/end() in the third alternative we must use
argument dependent lookup, but always considering 'std' as an associated
namespace. */
As you can see, they do nothing more than what the standard is actually describing.