Boost logo

Ublas :

From: Gunter Winkler (guwi17_at_[hidden])
Date: 2006-07-26 07:59:06

On Wednesday 26 July 2006 13:22, Georg Baum wrote:
> Another question: Why does the copy constructor have different semantics
> (concerning the dirty flag) than the assignment operator?

Because the do different things. Think of constructions like

M(i,j) = M(0,0);

here M(i,j) creates a proxy that hold the original value and dirty = false,
then the assignment operator changes the value and sets dirty=true, finally
the proxy is destructed and it moves the new value into the matrix M.

Now think of a function
template< class MATRIX >
void assign_value(MATRIX::reference Mij, const double value);

When you call
assign_value( M(i,j), 0);
then M(i,j) returns the proxy (by value, not a reference to the proxy) and Mij
is created using the copy constructor. Thus it must copy all information
including the dirty state before the old proxy is destructed. The same should
happen when you use

MATRIX::reference Mij( M(i,j) ); // this creates a reference
Mij = 0; // this assigns the value (as expected)

// this leaves Mij partially undefined and must not be used
MATRIX::reference Mij = M(i,j);

of course you can do bad things with undefined behavior like:
  MATRIX::reference Mij1( M(i,j) );
  MATRIX::reference Mij2( M(i,j) );

  Mij1 = 1; Mij2 = 2;
} // both proxy go out of scope and are destructed, but which is first?

This a limitation of (mutable) proxy classes one has to be aware of.