How do I know if object passed as r-value will get moved?

2917 views c++

I'm passing a lambda to a function which accepts it as a r-value reference.

If my lambda is defined in the function-call itself, I don't really care what happens to it later.
But if my lambda is a variable (say I want to use it more than once) I do want to know that it is not moved-from.

Is there a way to know will it be moved-from or not, making it unusable or usable accordingly after the call has returned?

answered question

Does the lambda capture some variables that have different move semantics compared to their copy semantics?

1 Answer


Well, if the function accept a universal reference and you want to forbid moving, there are many options for you to choose from, among others:

  1. Pass by constant reference (C++17 has std::as_const() to simplify that).
  2. Save it as a local and don't convert to rvalue-reference. Seems you do that.
  3. Use something to explicitly convert an rvalue-reference to an lvalue-reference.

Of course, if none of the variables captured by value by the lambda have different move-semantics than copy-semantics, the point is moot.

posted this

Have an answer?


Please login first before posting an answer.