[gnu.g++.lib.bug] Loop assignment of the vectors.

dl@ROCKY.OSWEGO.EDU (Doug Lea) (03/23/89)

[I took the liberty of redirecting this to the bug-lib-g++ list.]

>>	Hello, folks!  I have a question on the vector assignment in a loop
>>fashion.  Suppose we have a program segment as follows:
>>
>>intVec u(N), v(N), w;
>>
>>for (int i = 0; i < N; i++)
>>{
>>  w = u.at(i,2)+v.at(i,2);
>>}
>>
>>where, u.at(i,2) constructs a vector of 2 elements of u starting at u[i].
>>(This is from libg++ vector class.)

The question was, are the temporary objects constructed from the
u.at(i, 2) and v.at(i, 2) deleted after they are used each time
through the loop. The answer in g++ (at least since version 1.27 or
so) is yes, you can absolutely rely on destructors to be called for
temporary objects as soon as their syntactic lifetimes elapse. (In
this case, for example, they are destroyed after the operator +
constructs the sum but *before* the intVec::operator = assigns the sum
to w. The sum, in turn is destroyed after the assignment.) g++'s
behavior in this regard is agressive (i.e., it calls destructors at
the first possible opportunity) and highly predictable.

What happens to `w' depends on the nature of the class's operator =.
In the case of intVecs here, space is deleted and reallocated in
the normal manner, as you can tell by looking at intVec::operator = (intVec&)
in the .h file.

-Doug