Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #88205
| From | Keith Thompson <Keith.S.Thompson+u@gmail.com> |
|---|---|
| Newsgroups | comp.lang.c++ |
| Subject | Re: Sum an array in a lambda. |
| Date | 2022-12-21 10:32 -0800 |
| Organization | None to speak of |
| Message-ID | <878rj0r4k4.fsf@nosuchdomain.example.com> (permalink) |
| References | <mMCcnTVp9u2wdzz-nZ2dnZfqnPudnZ2d@giganews.com> <99ab1824-5934-428c-a586-0ee640bd5684n@googlegroups.com> <97GdnQ9Gd6Qhpz7-nZ2dnZfqnPudnZ2d@giganews.com> |
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.
--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Working, but not speaking, for XCOM Labs
void Void(void) { Void(); } /* The recursive call of the void */
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