C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Another reason why we should have simple operator for forward (and move)

From: Ville Voutilainen <ville.voutilainen_at_[hidden]>
Date: Sun, 8 Oct 2023 18:27:30 +0300
On Sun, 8 Oct 2023 at 18:25, Frederick Virchanza Gotham via
Std-Proposals <std-proposals_at_[hidden]> wrote:
>
> For quite a while now, a few people have been saying that we should
> have simple shorthand for 'forward', for example the following:
>
> template<typename T>
> void Func(T &&arg)
> {
> SomeOtherFunc( forward<T>(arg) );
> }
>
> could be simplified to something like:
>
> template<typename T>
> void Func(T &&arg)
> {
> SomeOtherFunc( ^^arg );
> }
>
> Well today I thought of another reason why we should have this.
> Consider the following lambda:
>
> auto forwardingLambda = [](auto&& param) { f(std::forward<T>(param)); };
>
> It doesn't compile because we don't have 'T'. In order to keep the
> 'auto' in there, we would need to write:
>
> auto forwardingLambda = [](auto&& param) {
> f(std::forward<std::conditional<std::is_rvalue_reference<decltype(param)>::value,
> std::remove_reference<decltype(param)>::type,
> decltype(param)>::type>(param));
> };
>
> Ain't nobody got time for that. It would be better if we could just do:

Yeah, but we don't need to write that, because

auto forwardingLambda = [](auto&& param) {
f(std::forward<decltype(param)>(param)); };

works just fine.

Received on 2023-10-08 15:27:44