There is such code:
#include <iostream>
#include <string>
int returnnumber() { return 2; }
std::string returntext() { return "siema"; }
int main() {
std::cout << (returntext() += "cze") << std::endl; // siemacze
//std::cout << (returnnumber() += 2) << std::endl; error: lvalue required as left operand of assignment
return 0;
}
Why is it possible to change return value of std::string, but not int?
Because
std::stringis a class type with a defined+=operator as a member function.And the standard allows you to call member functions on rvalues.
A silly consequence of that is that:
compilation results:
However, compilers differ (or used to differ) about how strictly they applied this subtle rule, or if at all.