When is rvalue evaluated?

So, I understand what is s2associated with the expression s1 + s1, but is it evaluated at the point in time s2, or is it lazy and evaluated when called s2 += "Test";? Will it also s2store memory for the time line?

#include <iostream>
#include <string>

int main()
{
    std::string s1 = "Test";
    std::string&& s2 = s1 + s1;
    s2 += "Test";
    std::cout << s2 << '\n';
}
+6
source share
2 answers

s2associated with an expression s1 + s1, but whether it is evaluated at the time it s2is assigned

Yes.

Will it also s2store memory for the time line?

Exactly s2attached to the temporary std::string.

s1 + s1 std::string, s2 ( ). s2 += "Test"; operator+=() s2, .. std::string.

+16

, . ( ).

, , , , .

string operator+(...) pr-value ( rvalue). & &, .

, string& string::operator=(string&&) . , ++ 11 ( ++ 17 ) . copy/move elision (N) RVO, () .

+2

All Articles