Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #88220

Re: Sum an array in a lambda.

From Ben Bacarisse <ben.usenet@bsb.me.uk>
Newsgroups comp.lang.c++
Subject Re: Sum an array in a lambda.
Date 2022-12-22 12:42 +0000
Organization A noiseless patient Spider
Message-ID <87lemzsj7e.fsf@bsb.me.uk> (permalink)
References (1 earlier) <99ab1824-5934-428c-a586-0ee640bd5684n@googlegroups.com> <97GdnQ9Gd6Qhpz7-nZ2dnZfqnPudnZ2d@giganews.com> <878rj0r4k4.fsf@nosuchdomain.example.com> <6LadnTLge6Mobj7-nZ2dnZfqnPSdnZ2d@giganews.com> <e93558ce-937f-4e40-8a7a-a3a6d5ea6855n@googlegroups.com>

Show all headers | View raw


Öö Tiib <ootiib@hot.ee> writes:

> On Thursday, 22 December 2022 at 08:05:59 UTC+2, Joseph Hesse wrote:
>> On 12/21/22 12:32, Keith Thompson wrote: 
>> > Joseph Hesse <jo...@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. 
>> 
> That is not true. The x in that program is int array of 4 elements,
> not pointer. The array decays to pointer in lot of contexts but
> range based for is not one of those. It treats x as an array.
> <https://en.cppreference.com/w/cpp/language/range-for>

You are right (of course!) but it's by no means trivial to follow the
details.  The cppreference page gives an example, but it's explanation
is very hard to plough through for someone learning C++.  You'd have to
understand how

  auto&& __range = x;

works and what __range and __range + 4 mean following a declaration like
that.

Clearly (at least I think so) __range will be an rvalue reference to an
array of 4 ints, but when I try to ditch the auto with

  int (&&__range)[4] = x;

g++ complains that it "cannot bind rvalue reference of type ‘int
(&&)[4]’ to lvalue of type ‘int [4]’.  What is the type that is being
deduced for __range in

  auto&& __range = x;

?  (Using a lvalue reference works but that's not what the standard says
is going on with this range-based for statement.)

-- 
Ben.

Back to comp.lang.c++ | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


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