Date: Thu, 10 Apr 2025 13:14:38 +0200
I don't think it's worth pursuing this if it cannot work in the
general case, and it really cannot.
For example, the last mention of a variable could be within the body
of a loop. In that case, it can be used multiple times with only one
syntactical appearance. However, there are also idioms involving do {
... } while(false) loops, so just because a variable appears in a loop
body doesn't mean that it's used multiple times.
Another obvious problem with automatic forwarding is that some code
intentionally copies ranges. For example, I might want to provide
strong exception guarantees, and cannot do that when moving a range
throws. In that event, I may copy it, even though the function
template takes a forwarding reference.
There are probably many other edge cases where existing code would
either not benefit or be negatively impacted. It's a pretty complex
language feature, and you're getting very little bang for your buck
here. To resolve some of the cases involving loops and if statements,
you need something bordering on a borrow checker; all that just to not
write std::forward in a few places? It's not like std::forward is
something you need all the time: only in templates, and only if they
take forward references.
general case, and it really cannot.
For example, the last mention of a variable could be within the body
of a loop. In that case, it can be used multiple times with only one
syntactical appearance. However, there are also idioms involving do {
... } while(false) loops, so just because a variable appears in a loop
body doesn't mean that it's used multiple times.
Another obvious problem with automatic forwarding is that some code
intentionally copies ranges. For example, I might want to provide
strong exception guarantees, and cannot do that when moving a range
throws. In that event, I may copy it, even though the function
template takes a forwarding reference.
There are probably many other edge cases where existing code would
either not benefit or be negatively impacted. It's a pretty complex
language feature, and you're getting very little bang for your buck
here. To resolve some of the cases involving loops and if statements,
you need something bordering on a borrow checker; all that just to not
write std::forward in a few places? It's not like std::forward is
something you need all the time: only in templates, and only if they
take forward references.
Received on 2025-04-10 11:14:51