So I understand that s2
binds to the expression s1 + s1
, but is this evaluated at the time s2
is assigned or is it lazy and evaluated when s2 += "Test";
is called? And also would s2
hold memory for a temporary string?
#include <iostream>
#include <string>
int main()
{
std::string s1 = "Test";
std::string&& s2 = s1 + s1;
s2 += "Test";
std::cout << s2 << '\n';
}
s2
binds to the expressions1 + s1
, but is this evaluated at the times2
is assigned
Yes.
And also would
s2
hold memory for a temporary string?
Precisely, s2
is bound to a temporary std::string
.
s1 + s1
will produce a temporary std::string
, which will be bound to the reference s2
(and its lifetime is extended to the lifetime of the reference). Then s2 += "Test";
, performs operator+=()
on s2
, i.e. the temporary std::string
.