In Item 25(170p~171p) of More Effective C++, the code is showed below:
class Widget {
public:
void setName(const std::string& newName) // set from
{ name = newName; } // const lvalue
void setName(std::string&& newName) // set from
{ name = std::move(newName); } // rvalue
…
};
w.setName("Adela Novak");
With the version of setName taking a universal reference, the string literal "Adela Novak" would be passed to setName, where it would be conveyed to the assignment operator for the std::string inside w. w’s name data member would thus be assigned directly from the string literal; no temporary std::string objects would arise.
I dont't understand why "no temporary std::string objects would arise" if the version of setName taking a universal reference, is called. Shouldn't the newName be created as the a temporary std::string?
Aucun commentaire:
Enregistrer un commentaire