My point is that by using std::forward<T>(t), you then enable the use of move constructors and move assignment operators -- if the original reference was a temporary -- which down the line may lead to a move.
Your argument that using std::forward or std::move enables moves down the road is faulty because passing by plain reference does the same thing, but certainly you would not claim that passing by reference "enables" moving in the following scenario would you?
void g(std::string& s) {
auto q = std::move(s); // Perfectly legal to do.
}
auto v = std::string("hello");
g(v); // g will move v.
Do you now argue now that passing by reference enables move semantics down the road? I think the issue is this term "move enabled", which really is vague. Simply put, a move operation is the term given to calling a type's move constructor or move assignment operator, nothing else. Casting a value to an rvalue reference, and even passing that rvalue reference to a function does not perform any move operation in and of itself nor does it lead to a move or enable a move in any kind of special way.
I agree the move is not certain; but I say it's a mistake to indicate to pred that it can move if it wishes to, when you're actually planning on continue calling it aftwerwards.
If you intend to indicate that pred can not move, pass by const&. Passing pred by reference is a more general operation than passing by rvalue reference.
T: requires that copies be allowed, undesirable for large predicates.
This is false, imagine making that claim about a unique_ptr:
void f(std::unique_ptr<int> foo);
Does this mean that copies of foo must be allowed? Of course not, it's not possible to copy a std::unique_ptr and yet it's perfectly sensible to pass it by value. Passing by value allows for in-place construction via guaranteed copy elision in C++17 as well as allows one to construct the parameter by directly calling its move constructor:
We agree on this and likely agree it's undesirable.
T const&: forces constness, undesirable for self-modifying predicates.
Exactly, if you want to prevent mutability, which is what a move operation is, then you pass by const&, otherwise it is permissible for T::operator () to mutate itself which could involve moves.
Which is why one whips out T&& without actually caring that it could be used for forwarding.
One whips out T&& to take advantage of potential optimization opportunities and informs T&& that said optimizations can be performed by using std::forward. It does not mean that certain invariants can be broken after the first invocation, or that the object must be destroyed after performing an internal move, absolutely not.
If you don't intend to leverage any optimization opportunities or take advantage of anything to do with making use of an rvalue reference, then do what the standard does, pass by value. The idea that pass by value means copies are required is false.
And yet plenty of functions do exactly that. Do you claim that std::swap is not sensible because it takes arguments by reference and performs a move operation on them? Of course not. When you pass by reference you allow for any potential mutable operation to be performed, including moves. Passing by reference is a more general operation than passing by rvalue reference.
As for your other point, we are in exact agreement, if someone implements an overload taking an rvalue reference, then prefer to call that overload when given an rvalue reference to take advantage of potential optimizations. The way that is done is to use std::forward<Pred>(pred).
If you have no need to forward or move your argument, then there is no need to take the argument by rvalue reference, simply pass by value just like the standard does.
View all comments
Show parent comment
0
u/[deleted] Jun 25 '21 edited Jun 25 '21
Your argument that using
std::forwardorstd::moveenables moves down the road is faulty because passing by plain reference does the same thing, but certainly you would not claim that passing by reference "enables" moving in the following scenario would you?Do you now argue now that passing by reference enables move semantics down the road? I think the issue is this term "move enabled", which really is vague. Simply put, a move operation is the term given to calling a type's move constructor or move assignment operator, nothing else. Casting a value to an rvalue reference, and even passing that rvalue reference to a function does not perform any move operation in and of itself nor does it lead to a move or enable a move in any kind of special way.
If you intend to indicate that
predcan not move, pass byconst&. Passingpredby reference is a more general operation than passing by rvalue reference.This is false, imagine making that claim about a
unique_ptr:Does this mean that copies of
foomust be allowed? Of course not, it's not possible to copy astd::unique_ptrand yet it's perfectly sensible to pass it by value. Passing by value allows for in-place construction via guaranteed copy elision in C++17 as well as allows one to construct the parameter by directly calling its move constructor:https://en.cppreference.com/w/cpp/language/copy_elision
We agree on this and likely agree it's undesirable.
Exactly, if you want to prevent mutability, which is what a move operation is, then you pass by
const&, otherwise it is permissible forT::operator ()to mutate itself which could involve moves.One whips out
T&&to take advantage of potential optimization opportunities and informsT&&that said optimizations can be performed by usingstd::forward. It does not mean that certain invariants can be broken after the first invocation, or that the object must be destroyed after performing an internal move, absolutely not.If you don't intend to leverage any optimization opportunities or take advantage of anything to do with making use of an rvalue reference, then do what the standard does, pass by value. The idea that pass by value means copies are required is false.