Quick Q: When is an rvalue evaluated?

Quick A: When it is assigned.

Recently on SO:

When is an rvalue evaluated?

s2 binds to the expression s1 + s1, but is this evaluated at the time s2 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.

Add a Comment

Comments are closed.

Comments (0)

There are currently no comments on this entry.