Question

This question can be considered a follow-up to the following question: C++ temporary variable lifetime.

Qt containers support the stream-like initialization syntax. Now, when I write the following code, my QVector is destructed right after assignment and the reference becomes dangling.

const QVector<QString>& v = QVector<QString>() << "X" << "Y" << "Z";

Corresponding operator<< is implemented the following way:

inline QVector<T> &operator<< (const T &t)
{ append(t); return *this; }

As far as I know, 10.4.10 Temporary Objects states that the lifetime of a temporary object is extended to match the lifetime of the correspnding const reference to it.

However, in this case the temporary object QVector<QString>() is destructed earlier.

I guess that probably this happens due to the fact that the last operation returns a QVector<QString>& and shouldn't know anything about the lifetime of the temporary QVector<QString>, but this explanation isn't strict and might be wrong.

So, why does this happen?

Was it helpful?

Solution

The lifetime of a temporary is only extended if it is bound to a const-reference:

const QVector<QString>& v = QVector<QString>();

However, in your code you are not binding the temporary to anything. Rather, you're calling a member function (of the temporary), which returns a reference (to the temporary). The result of this function call is no longer a temporary object, but just a plain reference. The original temporary object expires at the end of the full expression in which it appears, and the reference v becomes dangling.

(In the new C++, it is possible to prohibit such "accidents" by virtue of rvalue-qualified member functions, i.e. you could =delete the rvalue version of the << operator.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top