Quick A: When it is assigned.
Recently on SO:
When is an rvalue evaluated?
s2binds to the expressions1 + s1, but is this evaluated at the times2is assignedYes.
And also woulds2hold memory for a temporary string?Precisely,
s2is bound to a temporarystd::string.
s1 + s1will produce a temporarystd::string, which will be bound to the references2(and its lifetime is extended to the lifetime of the reference). Thens2 += "Test";, performsoperator+=()ons2, i.e. the temporarystd::string.

Add a Comment
Comments are closed.