The problem is that constness makes no claims about aliasing. Here's
an example with just scalars. The const applied to y means that the
reference y will not be used for writing, but it does not mean that y
refers to a separate object than x. If const somehow meant "y" is
unchangeable, then you would get 2 for an answer here, instead of
1. Nevertheless this is a perfectly legal program.
#include <iostream>
void foo(int& x, const int& y) {
x = 1;
x = y;
}
int
main()
{
int x = 2;
foo(x, x);
std::cout << x << std::endl;
}
James A. Crotinger writes:
> on 3/23/00 1:13 PM, oon-digest at owner-oon-digest@oonumerics.org wrote:
>
> > void rank1Update(Matrix& A, const Vector& x)
> > {
> > for (int i=0; i < A.rows(); ++i)
> > for (int j=0; j < A.cols(); ++j)
> > A(i,j) += x(i) * x(j);
> > }
> >
> > [...] However, the compiler can't be sure that the data in the matrix A
> > doesn't overlap the data in the vector x -- there is a possibility that
> > writing to A(i,j) could change the value of x(i) partway through.
>
> This one has always bothered me - why isn't "const" enough to tell the
> compiler that x isn't being changed by the assignment.
>
> Jim
>
> --
> ------------------------------------------------------------------------
> James A. Crotinger Los Alamos National Lab email: jac@lanl.gov
> Technical Staff Member CIC-ACL, MS B287 phone: 505-665-6022
> Advanced Computing Lab Los Alamos, NM 87545 fax: 505-665-4939
>
> --------------------- Object Oriented Numerics List --------------------------
> * To subscribe/unsubscribe: use the handy web form at
> http://oonumerics.org/oon/
> * If this doesn't work, please send a note to owner-oon-list@oonumerics.org
>
--------------------- Object Oriented Numerics List --------------------------
* To subscribe/unsubscribe: use the handy web form at
http://oonumerics.org/oon/
* If this doesn't work, please send a note to owner-oon-list@oonumerics.org
This archive was generated by hypermail 2b29 : Wed Feb 20 2002 - 03:20:10 EST