The answer to this question points out that you shouldn't return references to parameters, as they may be temporaries which may be out of scope, and will not be preserved if the result is assigned to a reference.
Following that logic, doesn't the following code have a similar problem?
#include <iostream>
class T
{
public:
T& operator+=(const T& t) { this->x += t.x; return *this; }
T(int x) : x(x) {}
int x;
~T()
{
std::cout << "My value is " << this->x << " and I'm dead." << std::endl;
this->x = -999;
}
};
int main()
{
const T& t = (T(2) += T(3));
std::cout << t.x << std::endl;
}
And if so, how should I write the +=
operator to avoid this problem?
I also imagine there would be problems with the following code in C++11, but I haven't checked it:
#include <iostream>
#include <string>
int main()
{
for ( char x : std::string("hello") += std::string("world") )
{
std::cout << x << std::endl;
}
}
It would seem as a result you should never return references to function parameters, unless you are prepared to risk undefined behaviour in range based for loops.