Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #88212
| Date | 2022-12-22 00:05 -0600 |
|---|---|
| Subject | Re: Sum an array in a lambda. |
| Newsgroups | comp.lang.c++ |
| References | <mMCcnTVp9u2wdzz-nZ2dnZfqnPudnZ2d@giganews.com> <99ab1824-5934-428c-a586-0ee640bd5684n@googlegroups.com> <97GdnQ9Gd6Qhpz7-nZ2dnZfqnPudnZ2d@giganews.com> <878rj0r4k4.fsf@nosuchdomain.example.com> |
| From | Joseph Hesse <joeh@gmail.com> |
| Message-ID | <6LadnTLge6Mobj7-nZ2dnZfqnPSdnZ2d@giganews.com> (permalink) |
On 12/21/22 12:32, Keith Thompson wrote:
> Joseph Hesse <joeh@gmail.com> writes:
>> On 12/20/22 11:29, Öö Tiib wrote:
>>> auto fp = [] (int x[])
>>> ... is by language rules equivalent to ...
>>> auto fp = [] (int *x)
>>> ... so array length information is lost and range
>>> based for has no idea what range you mean.
>>>
>>>>
>>>
>> In the following program, the array definition and range based for
>> loop are in the same scope so it appears that the range based for loop
>> sees the size of the array. The following program compiles with the
>> gnu compiler and runs correctly. I am surprised that it works.
>> =========================================
>> #include <iostream>
>> #include <vector>
>> using namespace std;
>> int main(){
>> int x[4] = {1, 2, 3, 4};
>> int sum = 0;
>> for(const int &i : x)
>> sum += i;
>> cout << "sum = " << sum << '\n';
>> return 0;
>> }
>
> Why are you surprised?
>
> In `auto fp = [] (int x[])`, x is an array parameter, which is treated
> as a pointer parameter. That equivalence applies only to function
> parameters. In your example, you just have an array object.
>
In the above program the x in the for loop is treated as
an int *. The fact that the program works means that the
for loop knows how far to increment the pointer to calculate
the sum. This is what surprises me, I thought the only information
a built in array type contains is a pointer to the first element.
Thank you,
Joe
Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Sum an array in a lambda. Joseph Hesse <joeh@gmail.com> - 2022-12-20 11:00 -0600
Re: Sum an array in a lambda. Öö Tiib <ootiib@hot.ee> - 2022-12-20 09:29 -0800
Re: Sum an array in a lambda. Joseph Hesse <joeh@gmail.com> - 2022-12-21 10:56 -0600
Re: Sum an array in a lambda. Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2022-12-21 10:32 -0800
Re: Sum an array in a lambda. Joseph Hesse <joeh@gmail.com> - 2022-12-22 00:05 -0600
Re: Sum an array in a lambda. Öö Tiib <ootiib@hot.ee> - 2022-12-22 00:00 -0800
Re: Sum an array in a lambda. Ben Bacarisse <ben.usenet@bsb.me.uk> - 2022-12-22 12:42 +0000
Re: Sum an array in a lambda. Paavo Helde <eesnimi@osa.pri.ee> - 2022-12-20 20:03 +0200
Re: Sum an array in a lambda. Juha Nieminen <nospam@thanks.invalid> - 2022-12-21 06:35 +0000
Re: Sum an array in a lambda. Bonita Montero <Bonita.Montero@gmail.com> - 2022-12-22 19:55 +0100
csiph-web