1

Is it a good idea to pass parameters as rvalue references just to make it clear that you're going to manipulate the argument? Kinda like the opposite of const correctness.

For example, this function that takes a vector of strings, manipulates it somehow and returns a boolean value:

static bool isThisSomething(std::vector<string>&& input1);

Now if the caller wants to call this function with an lvalue, he has to use std::move() and acknowledge that this vector will be messed with. This should prevent unexpected side effects.

std::vector<std::string> lvalueVector = {"foo", "bar"};
bool somethingness = isThisSomething(std::move(lvalueVector)); 

Clarification:
The manipulations that isThisSomething does, are just part of some internal logic and would appear like nonsense for the caller.

Iizuki
  • 354
  • 1
  • 6
  • 12
  • 1
    What "manipulation"? Why not take by value if the caller doesn't care what you do to compute that boolean, so the function gets its own copy to change as it sees fit? Would that really result in copies so common/expensive that you need to force all callers to agree to move in and potentially "mess with" their vectors? – underscore_d Jun 29 '20 at 12:12
  • It might get manipulated, but after that your `lvalueVector` will be empty. The changes will not be reflected – Waqar Jun 29 '20 at 12:14
  • 1
    @Waqar It won't necessarily be empty. It will just be in a moved-from, i.e. "valid but unspecified" state. One cannot rely on it being empty, but one should just discard or reassign it in order to continue using it. – underscore_d Jun 29 '20 at 12:15
  • 1
    moving is not the same as "manipulatie the argument". I'd first consider if you need a copy or not and only then consider to supply rvalue ref overload in case a copy is expensive – 463035818_is_not_an_ai Jun 29 '20 at 12:16
  • @underscore_d yes, what i mean is that the value inside it will not be there after moving. But It will be in a valid state, yes. – Waqar Jun 29 '20 at 12:17
  • @Waqar Yes, that was intentional. Maybe "manipulate" was a poor choice of words. The point is that the function changes the argument as part of it's internal implementation. – Iizuki Jun 29 '20 at 12:19

1 Answers1

5

Is it a good idea to pass parameters as rvalue references just to make it clear that you're going to manipulate the argument?

Not in general. In particular, it depends on how you are manipulating the argument.

If you accept a parameter by rvalue, then you are implying that the argument will be left in an unspecified, typically moved-from state. If that is what the function does, then rvalue reference is indeed appropriate. Note that depending on use case, passing the argument by value is often a good alternative in this case.

That is the opposite implication that you would want to make when you modify the object such that the caller might find the modified state useful. Lvalue reference to non-const implies that the argument will be manipulated in this way.

eerorika
  • 232,697
  • 12
  • 197
  • 326
  • Could you elaborate on when it makes sense to pass by value vs. pass by rvalue reference? – Iizuki Jun 29 '20 at 15:24
  • 2
    @Iizuki If you want to be able to both pass an rvalue by move, or pass an lvalue by copy, then passing a value achieves both. Rvalue argument does only former. – eerorika Jun 29 '20 at 15:26