C ++ semantics transfer

I have a Text class containing std :: string. SetText method as follows:

void Text::SetText( const std::string& str )
{
    m_str = str;
}

Since this method is almost always called with rvalues ​​parameters as a parameter, I was thinking about move constructors. I understand the basics, but that’s it. So I did tests and came to the conclusion that another function like this would be better, and once the move and move constructors were defined, it would be possible to increase performance:

void Text::SetText( std::string&& str )
{
    m_str = move( str );
}

Have my questions:

  • Does this work with std container? Do they provide a constructor for relocation and assignment?
  • Is move semantics useful when there are no heap allocations - is this a class? (I mean the lack of heap allocation at all, so no smart pointers as a member of the class)

Thank.

+4
1

. ++ 11:

void Text::SetText(std::string str)
{
    m_str = move(str);
}

, .

, rvalue , lvalue, .

std? ?

, , , .

, , ? ( , )

, .

, , .

, inline, rvalue. lvalue, operator=, .

+7

All Articles