C++ Logo

std-proposals

Advanced search

Re: Operator for reference casting

From: Jason McKesson <jmckesson_at_[hidden]>
Date: Sun, 17 Jan 2021 12:28:37 -0500
On Sun, Jan 17, 2021 at 8:01 AM Jean-Baptiste Vallon Hoarau via
Std-Proposals <std-proposals_at_[hidden]> wrote:
>
> Jason :
>
> I suppose you propose to automatically forward the variable at the last use and not before?

No, it would always forward. Remember: forwarding doesn't do anything
in and of itself. What does something is how the recipient of the
forwarding process uses it.

The above example of `std::size` won't actually break anything because
`std::size` doesn't modify its parameter. Same goes for a (reasonable)
logging function.

If you use a forwarded parameter in a way that will logically consume
the parameter, then you should only use it that way once. And that's
still on your to do correctly.

Now, a smart compiler could check to see if you're forwarding the
value to a function that takes a forwarding reference or if overload
resolution will select an rvalue reference function or move from an
rvalue reference into a value parameter or something, and then assume
that this particular variable has been moved from and give you a
warning for future uses of it. But the meaning of a piece of syntax
should not change based on where it is in a function or what follows
it.

This is also why we shouldn't allow implicit moves on the last use of
a local variable in a function, even if a move operation was available
to that usage. `return name` is OK to move from because that statement
will *always* be the last thing executed in the function no matter
where it appears relative to other code. But moving code around
shouldn't affect the behavior in terms of overload resolution and the
like.

Received on 2021-01-17 11:28:51