> > This one has always bothered me - why isn't "const" enough to tell the
> > compiler that x isn't being changed by the assignment.
Todd gave a good answer for Vector. Let me point out that even without
multilevel indirection, const still has nothing to do with aliasing.
Suppose you were programming in plain C and wrote:
void rank1Update(float* A, const float * x, int n ) {
int i, j;
for( i=0; i<n; ++i )
for( j=0; j<n; ++j )
A[i*n+j] += x[i]*x[j];
}
Even though we have just a single level of indirection here, the compiler
cannot determine whether A and x are aliased without analyzing the call sites,
because C/C++ allow this call:
void foo(float* z) {
rank1Update(z,z,10);
}
The qualifier "const" on the target *p of a pointer p simply says that *p
cannot be modified via pointer p. It does not prohibit modifying the
object pointed to by p via some other pointer.
Also, note that "const" can be cast away.
For how little const promises, see
Andrew Koenig, ``How much does const promise?'', C++ Report, May 1997, 9(5), pp.7-8.
Const is almost useless for optimization. Consider a global declaration:
const Foo x(2)
Do I know that x never changes after initialization? I don't, because I could
have:
Foo * Munger;
struct Foo {
Foo( int j ) {...; Munger = this;}
};
That is, the constructor might "capture" the pointer to x for use later.
As a writer of an optimizing compiler, I have to ignore "const",
except for global variables declared as const that have static initializers.
It's useless otherwise.
Arch D. Robison Kuck & Associates Inc.
robison@kai.com 1906 Fox Drive
217-356-2288 ext. 56 Champaign IL 61820
Lead Developer for KAI C++ http://www.kai.com/C_plus_plus/index.html
--------------------- 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