Quick Q: Why pass by value then move? -- StackOverflow

Quick A: Because it's a new C++11 parameter passing pattern, also recently mentioned as part of GotW #4.

Why do we copy then move?

I saw code somewhere in which someone decided to copy an object and subsequently move it to a data member of a class. This left me in confusion in that I thought the whole point of moving was to avoid copying. Here is the example:

 

struct S
{
    S(std::string str) : data(std::move(str))
    {}
};

Here are my questions:

  • Why aren't we taking an rvalue-reference to str?
  • Won't a copy be expensive, especially given something like std::string?
  • What would be the reason for the author to decide to make a copy then a move?
  • When should I do this myself?

Add a Comment

Comments are closed.

Comments (0)

There are currently no comments on this entry.