John Leidegren
John Leidegren

Reputation: 60987

When is std::move redundant?

Simply put I have this class in which I'd like to move the wrapped object.

std::string m_S;
string_t(string_t&& s)
  : m_S(s.m_S)
{
}

I omitted the surrounding class structure, just added the member m_S for completeness sake.

Am I required to wrap the member access with a std::move or will this just work since s is passed as a rvalue reference?

Upvotes: 3

Views: 597

Answers (2)

Deduplicator
Deduplicator

Reputation: 45654

If the function-arguments type is rvalue-reference, the argument is still an lvalue inside the function (as well as for ctors init-list).

Thus, you really need to use std::move or equivalent if you mean it.

Upvotes: 1

Mike Seymour
Mike Seymour

Reputation: 254431

You do need move here. s.m_S is an lvalue expression, so will not bind to an _rvalue_ reference.

Even s itself is an lvalue expression, so you'd also need move if you wanted to use that to move-construct a variable.

Upvotes: 6

Related Questions