public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug middle-end/54299] New: Array parameter does not allow for iterator syntax
@ 2012-08-17 15:26 drepper.fsp at gmail dot com
  2012-08-17 15:40 ` [Bug middle-end/54299] " redi at gcc dot gnu.org
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: drepper.fsp at gmail dot com @ 2012-08-17 15:26 UTC (permalink / raw)
  To: gcc-bugs

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: unassigned@gcc.gnu.org
        ReportedBy: drepper.fsp@gmail.com


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);
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug middle-end/54299] Array parameter does not allow for iterator syntax
  2012-08-17 15:26 [Bug middle-end/54299] New: Array parameter does not allow for iterator syntax drepper.fsp at gmail dot com
@ 2012-08-17 15:40 ` redi at gcc dot gnu.org
  2012-08-17 15:43 ` redi at gcc dot gnu.org
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: redi at gcc dot gnu.org @ 2012-08-17 15:40 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=54299

--- Comment #1 from Jonathan Wakely <redi at gcc dot gnu.org> 2012-08-17 15:40:42 UTC ---
I think the compiler's correct to reject the first example, the type of arr is
int* so it's not an array, and so the for loop calls begin(arr) and end(arr)

In the second example the parameter type is int(*)[10] so arr[0] is an array of
known bound, and so the for loop works.


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug middle-end/54299] Array parameter does not allow for iterator syntax
  2012-08-17 15:26 [Bug middle-end/54299] New: Array parameter does not allow for iterator syntax drepper.fsp at gmail dot com
  2012-08-17 15:40 ` [Bug middle-end/54299] " redi at gcc dot gnu.org
@ 2012-08-17 15:43 ` redi at gcc dot gnu.org
  2022-01-06  3:44 ` [Bug c++/54299] " pinskia at gcc dot gnu.org
  2022-01-06  3:46 ` pinskia at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: redi at gcc dot gnu.org @ 2012-08-17 15:43 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=54299

--- Comment #2 from Jonathan Wakely <redi at gcc dot gnu.org> 2012-08-17 15:43:02 UTC ---
It would be nice if G++ told you the type of the range, as Clang++ does:

a.cc:9:15: error: use of undeclared identifier 'begin'
  for (auto i : arr)
              ^
a.cc:9:15: note: range has type 'int *'


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug c++/54299] Array parameter does not allow for iterator syntax
  2012-08-17 15:26 [Bug middle-end/54299] New: Array parameter does not allow for iterator syntax drepper.fsp at gmail dot com
  2012-08-17 15:40 ` [Bug middle-end/54299] " redi at gcc dot gnu.org
  2012-08-17 15:43 ` redi at gcc dot gnu.org
@ 2022-01-06  3:44 ` pinskia at gcc dot gnu.org
  2022-01-06  3:46 ` pinskia at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-01-06  3:44 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=54299

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Last reconfirmed|                            |2022-01-06
             Status|UNCONFIRMED                 |NEW
     Ever confirmed|0                           |1

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Clang gives even better diagnostic now:
<source>:7:17: error: cannot build range expression with array function
parameter 'arr' since parameter with array type 'int[10]' is treated as pointer
type 'int *'
  for (auto i : arr)
                ^~~
<source>:4:11: note: declared here
int f(int arr[10])
          ^


GCC trunk gives:
<source>: In function 'int f(int*)':
<source>:7:17: error: 'begin' was not declared in this scope
    7 |   for (auto i : arr)
      |                 ^~~
<source>:7:17: error: 'end' was not declared in this scope

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug c++/54299] Array parameter does not allow for iterator syntax
  2012-08-17 15:26 [Bug middle-end/54299] New: Array parameter does not allow for iterator syntax drepper.fsp at gmail dot com
                   ` (2 preceding siblings ...)
  2022-01-06  3:44 ` [Bug c++/54299] " pinskia at gcc dot gnu.org
@ 2022-01-06  3:46 ` pinskia at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-01-06  3:46 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=54299

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|normal                      |enhancement

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2022-01-06  3:46 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-08-17 15:26 [Bug middle-end/54299] New: Array parameter does not allow for iterator syntax drepper.fsp at gmail dot com
2012-08-17 15:40 ` [Bug middle-end/54299] " redi at gcc dot gnu.org
2012-08-17 15:43 ` redi at gcc dot gnu.org
2022-01-06  3:44 ` [Bug c++/54299] " pinskia at gcc dot gnu.org
2022-01-06  3:46 ` pinskia at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).