Reputation: 163
There is my code
int array[3][4] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
for (auto line : array)
// something
Why the type of line
is int *
instead of int (*)[4]
?
I have looked for reference about the range-based for loop, and I'm using C++ 11, so the corresponding source code is
{
auto && __range = range_expression ;
for (auto __begin = begin_expr, __end = end_expr; __begin != __end; ++__begin) {
range_declaration = *__begin;
loop_statement
}
According to the first item of the Explanation:
If range_expression is an expression of array type, then begin_expr is
__range
so there is a satement __begin = range_expression
, and it equal to auto line = array
.
and then the type of line
is int (*)[4]
, but in fact it's int *
?
Why? Could anybody help me?
Upvotes: 2
Views: 67
Reputation: 2917
In your example __begin
is of type int(*)[4]
range_declaration
is the dereferenced value of __begin
, therefore in your example range_declaration
is of type int*
.
In your example, the line
variable is the range_declaration
.
If we remove the syntactic sugar, your code looks like this:
int array[3][4] = {{1, 2, 3, 0}, {4, 5, 6, 0}, {7, 8, 9, 0}};
{
int (&__range)[3][4] = array;
int (*__begin)[4] = __range;
int (*__end)[4] = __range + 3L;
for(; __begin != __end; ++__begin) {
int * line = *__begin;
}
}
Upvotes: 2