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


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

Re: Sum an array in a lambda.

From Juha Nieminen <nospam@thanks.invalid>
Newsgroups comp.lang.c++
Subject Re: Sum an array in a lambda.
Date 2022-12-21 06:35 +0000
Organization Aioe.org NNTP Server
Message-ID <tnu9f7$vn7$1@gioia.aioe.org> (permalink)
References <mMCcnTVp9u2wdzz-nZ2dnZfqnPudnZ2d@giganews.com>

Show all headers | View raw


Joseph Hesse <joeh@gmail.com> wrote:
>   auto fp = [] (vector<int> vi)

By the way, you should almost never take class instances like this
as function parameters by value. You should almost always take them
as const reference.

When you take it by value like that, it will be *copied* for the
function. The larger the vector is, the heavier it becomes to copy.
In most cases (like here) the function doesn't need a copy of the
vector. A reference to the original suffices (and passing a reference
as parameter to the function is infinitely more efficient than
copying the entire vector).

The only case where you want to pass such an object by value instead
of by reference is when the function actually needs a deep-copy of
the object, but that's quite rare. (Even in such cases it's actually
usually better to still take the parameter by const reference and
copy it inside the function.)

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