Moving arguments or passing them in the usual way C ++ 98/03?

Suppose there is a class Person:

// "string" is std::string, "move" is std::move

class Person
{
public:
    // C++11 way: pass by value and std::move() from the value
    Person(string name, string surname)
        : m_name(move(name)), m_surname(move(surname))
    {}

    ....

private:
    string m_name;
    string m_surname;
};

Suppose I read values nameand surnamefrom some source (such as a file) in a cycle, and each iteration of the loop I want to create Personwith these read values (and, for example, the press created Personin a container).

Is it better to call the constructor in the Personusual way:

Person(name, surname)

or using std::movefor arguments?

Person(move(name), move(surname))
+3
source share
4 answers

I would recommend not to make it difficult, deciding to use it Person(move(name), move(surname))without special reasons for this.

, Person:

string name, surname;
while ((cin >> name) && (cin >> surnam)) {
  Person p(name, surname);
  // ...
}

name surname Person, . name surname , , , , , . name surname, .

move(), , , , . , move() .

, , , , "" string. , "" Person.


, , . , move() .

+7

, name surname. , ( move , ).

+5

, . , , - , rvalue ( rvalue/lvalue 3 ). / .

Nevermind, . ++ 11 way = pass-by-value-then-move, , - . , rvalue.

, , .

: . : ++ 11 VS

+2

, , , . :

Person(const string &name, const string &surname)
        : m_name(name), m_surname(surname)

- , , std::move.

0

All Articles