When is std :: move redundant?

Simply put, I have this class that I would like to move the wrapped object into.

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

      

I've omitted the structure of the surrounding class, just added an element m_S

for completeness.

Do I need to wrap the member access with std::move

or will this work as it s

is passed as an rvalue reference?

+3


source to share


2 answers


This is where you need move

. s.m_S

is an lvalue expression, so it is not referenced _rvalue_

.



Even s

itself is an lvalue expression, so you'll also need move

it if you want to use it to move-construct a variable.

+6


source


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



So you really need to use std::move

or the equivalent if that's what you mean.

+1


source







All Articles