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


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

Re: R-value references and std::forward

From Juha Nieminen <nospam@thanks.invalid>
Newsgroups comp.lang.c++
Subject Re: R-value references and std::forward
Date 2022-12-14 12:41 +0000
Organization Aioe.org NNTP Server
Message-ID <tncgah$1tol$1@gioia.aioe.org> (permalink)
References <6d19a662-76a1-4925-bd07-b259836f062bn@googlegroups.com> <jvtj6mFq5jfU1@mid.individual.net> <a4ffe6c9-868a-4a12-9ab2-49094d7466c8n@googlegroups.com>

Show all headers | View raw


Frederick Virchanza Gotham <cauldwell.thomas@gmail.com> wrote:
> If I knew for certain that it was an "int&" then why would I bother
> using 'forward' on it?

std::forward is useful mainly with templated types. It's not all that
useful with concrete types.

The main idea with std::forward is that it preserves lvalues as lvalues
and rvalues as rvalues when passing parameters to other functions (if
you don't use std::forward and tried to pass the function parameter
directly then an rvalue would decay to an lvalue, because a named
variable always decays to an lvalue, unless explicitly cast to an
rvalue). In order to do this you need to provide it with the original
type of the value you are passing, as a template parameter. This
would usually be the original template type that that parameter has.

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


Thread

R-value references and std::forward Frederick Virchanza Gotham <cauldwell.thomas@gmail.com> - 2022-12-14 00:44 -0800
  Re: R-value references and std::forward Bo Persson <bo@bo-persson.se> - 2022-12-14 10:26 +0100
    Re: R-value references and std::forward Frederick Virchanza Gotham <cauldwell.thomas@gmail.com> - 2022-12-14 01:48 -0800
      Re: R-value references and std::forward Bo Persson <bo@bo-persson.se> - 2022-12-14 12:42 +0100
        Re: R-value references and std::forward Frederick Virchanza Gotham <cauldwell.thomas@gmail.com> - 2022-12-14 06:46 -0800
          Re: R-value references and std::forward Öö Tiib <ootiib@hot.ee> - 2022-12-15 05:48 -0800
      Re: R-value references and std::forward Juha Nieminen <nospam@thanks.invalid> - 2022-12-14 12:41 +0000
        Re: R-value references and std::forward Bonita Montero <Bonita.Montero@gmail.com> - 2022-12-15 19:03 +0100
  Re: R-value references and std::forward Bonita Montero <Bonita.Montero@gmail.com> - 2022-12-15 16:39 +0100
  Re: R-value references and std::forward Andrey Tarasevich <andreytarasevich@hotmail.com> - 2022-12-17 10:55 -0800

csiph-web