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


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

Re: Sum an array in a lambda.

From Paavo Helde <eesnimi@osa.pri.ee>
Newsgroups comp.lang.c++
Subject Re: Sum an array in a lambda.
Date 2022-12-20 20:03 +0200
Organization A noiseless patient Spider
Message-ID <tnstee$oct8$1@dont-email.me> (permalink)
References <mMCcnTVp9u2wdzz-nZ2dnZfqnPudnZ2d@giganews.com>

Show all headers | View raw


20.12.2022 19:00 Joseph Hesse kirjutas:
> I want to sum an array of int's in a lambda function.
> 
> In the following code, function f1 does this with no problem.
> 
> In function f2, I am able to sum an int array using a range based
> for loop.  That this works surprises me since the array name is not 
> converted to a pointer and the for loop "looks around" to find the
> size of int x[].
> 
> The commented out code in f2 was my attempt, as in f1, to
> put the code to sum the array in a lambda.  It does not compile.
> 
> Is it possible to make this work?
> 
> Thank you,
> Joe
> =======================================================
> #include <iostream>
> #include <vector>
> using namespace std;

> 
> void f2(){
>    int x[4] = {1, 2, 3, 4};
> 
>    int sum = 0;
>    for(const int &i : x)
>        sum += i;
>    cout << "sum = " << sum << '\n';
> 
> /*
>    auto fp = [] (int x[])
>    {
>      int sum = 0;
>      for(const int &i : x)
>        sum += i;
>      return sum;
>    };
> 
>    cout << "sum = " << fp(x) << '\n';
> */
> }

You can fix it easily by over-using auto:

void f2() {
     int x[4] = { 1, 2, 3, 4 };

     auto fp = [] (const auto& x)
       {
         int sum = 0;
         for(const int &i : x)
           sum += i;
         return sum;
       };

       std::cout << "sum = " << fp(x) << '\n';

}


However, using C arrays seems fragile in general as they decay to 
pointers too easily. This seems better:

void f2() {
     std::array<int, 4> x = { 1, 2, 3, 4 };

       auto fp = [] (const auto& range)
       {
         int sum = 0;
         for(const int &i : range)
           sum += i;
         return sum;
       };

       std::cout << "sum = " << fp(x) << '\n';

}







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