http://gcc.gnu.org/bugzilla/show_bug.cgi?id=54299
Bug #: 54299
Summary: Array parameter does not allow for iterator syntax
Classification: Unclassified
Product: gcc
Version: 4.7.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: middle-end
AssignedTo: [email protected]
ReportedBy: [email protected]
Compile the following code:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
int aa[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int f(int arr[10])
{
int s = 0;
for (auto i : arr)
s += i;
return s;
}
int main()
{
return f(aa);
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This fails with
u.cc: In function ‘int f(int*)’:
u.cc:18:17: error: ‘begin’ was not declared in this scope
u.cc:18:17: error: ‘end’ was not declared in this scope
u.cc:18:17: error: unable to deduce ‘auto’ from ‘<expression error>’
This indicates that the problem is that the parameter is seen as 'int *'
instead of as 'int [10]'. According to Andrew another problem caused by the
too-early decay of arguments to pointers (bug 24666).
Changing the code as follows makes it compile:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
int aa[1][10] = { { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 } };
int f(int arr[1][10])
{
int s = 0;
for (auto i : arr[0])
s += i;
return s;
}
int main()
{
return f(aa);
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~