C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Automatic perfect forwarding is possible and not too complicated

From: Ell <ell_se_at_[hidden]>
Date: Fri, 11 Apr 2025 11:55:14 +0300
On 4/11/25 00:22, Frederick Virchanza Gotham via Std-Proposals wrote:
> On Thu, Apr 10, 2025 at 3:03 PM Ell wrote:
>>
>> As already been said, this creates more problems than it solves, but if
>> you *did* want something like this, why limit it to forwarding
>> references? Why not allow any variable to be moved upon last use
>> (basically implicit-move on steroids)?
>
>
> You mean something like the following?
>
> extern string Some_Library_Function(void); // defined in a shared
> library binary file
>
> template<typename T> requires is_same_v< string, remove_cvref_t<T> >
> void LibFunc1(T &&arg) { /* Do something */ }
>
> template<typename T> requires is_same_v< string, remove_cvref_t<T> >
> void LibFunc2(T &&arg) { /* Do something */ }
>
> template<typename T> requires is_same_v< string, remove_cvref_t<T> >
> void LibFunc3(T &&arg) { /* Do something */ }
>
> int main(void)
> {
> string s = Some_Library_Function();
>
> string &&&ss = s;
>
> LibFunc1(s2);
> LibFunc2(s2);
> LibFunc3(s2); // This one is automatically forwarded
> }

I mean that wanting to move a variable upon last use is independent of
whether it's a reference or a function parameter. In your example, why
should you need to introduce a reference in the first place? You might
want to explicitly mark the variable somehow to opt into this
behavior... or maybe not? Like others already said, even the former can
already lead to unwanted surprises.

Received on 2025-04-11 08:55:25