I am trying to understand the usage of std::move. Could you please tell me if my reasoning is right? I consider the following class:
class T{
public:
T(string s){
str = move(s);
}
string str;
};
Now let's consider two ways of using this class.
T t("abc");
Here what happens is that string "abc" is first created, and then its resource is moved to t.str. Therefore string "abc" is never copied.
string s = "abc"; T t(s);
Here, first, string s is created. Then a copy of s is passed by value to the constructor T(). Finally the resource of the copy of s is moved to t.str. In total "abc" is copied once.
Is this true?
std::stringto somehow share its storage with the string literal, with no copying, so at least one copy is inevitable. In the second example, there are two copies because in the end there are two named objects of typestd::string, with the same contents.T(string&& s) : str(std::move(s)) {}string s("abc");would your answer still be the same?