Collectives: why use rvalue references and not const reference ?
Issue #96
resolved
My discussion with P. Ghysels led to the following comment:
Why do we have: future<T> broadcast(T && val, intrank_t sender);
instead of : future<T> broadcast(const T & val, intrank_t sender);
if "val" is never modified ? The second signature should also be able to handle rvalues, right ?
Comments (3)
-
-
- changed component to Teams and Collectives
-
- changed status to resolved
I believe this issue is resolved.
If not, please re-open with details.
- Log in to comment
In the context where we use &&, it doesn't necessarily result in an rvalue reference. Since the
T
is a template parameter,T&&
is what's often called a universal reference. The result is that if the user passes an lvalue, the argument type deduces asconst T&
, but if the user passes an rvalue, it deduces asT&&
. The latter allows us to move the rvalue into upcxx-managed storage instead of having to make a copy, which is especially important if the value is expensive to copy. On the other hand, we can't move aconst T&
.