C++ Logo


Advanced search

Re: [std-proposals] Specify that argument to function cannot be an rvalue

From: Lénárd Szolnoki <cpp_at_[hidden]>
Date: Tue, 11 Oct 2022 17:25:01 +0100

On 11 October 2022 17:08:59 BST, Thiago Macieira via Std-Proposals <std-proposals_at_[hidden]> wrote:
>On Tuesday, 11 October 2022 04:30:52 PDT Ville Voutilainen via Std-Proposals
>> void f(int&&) { puts("int"); }
>> void f(double&&) { puts("double"); }
>> so
>> int i = 1;
>> f(i);
>> prints "double", because it can't call void f(int&&). Instead, it'll
>> convert the int to double,
>> and call void f(double&&).
>But why can't it convert int to int and then call the int&& overload?

Right. This could replace "pass by value then move" and save a move when a user passes an xvalue. Now it requires an overload or a template to achieve the same thing.

I guess rvalue refs binding to temporaries of the same type as the argument could be surprising and possibly a footgun in a different way. But the current overload behavior is also surprising.

Changing the current behavior is probably not possible now. It would be nice to have tooling to be able to have non-template `void foo(??create_obj_if_necessary?? T&&)` (not proposing this syntax and name) that would have the behavior you describe. I tried to have library solution for it, but it doesn't seem possible.


Received on 2022-10-11 16:25:04