Re: OON: Re: Aliasing and const

From: Oliver Axel Ruebenacker (oliver@physics.umass.edu)
Date: Sat Mar 25 2000 - 10:23:26 EST


        Hello!

> > 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;}
> > };

  Unless, of course, "this" is of type "const Foo*", then the above would
gives at least a warning.
  Instead of introducing a keyword like restrict, wouldn't it be easier to
make it illegal to make a non-const alias of a const object, and for a
const class object, to only use const member functions, constructor and
destructor? The GNU C++ compiler issues warnings (e.g. with option -Wall)
if you try to make a non-const alias of a const object. This includes
function parameters, so you should not do this:

  const int x; void f(int & y) {...}; f(x)

  I never had a case where I felt like breaking this restriction. Since
most of the functions I write take parameters as (const&), all I would
need is to tell my compiler that it can rely on const. Or am I wrong?
  The only sad thing I know about is that with non-trivial assignment
operators (=, +=, /=, -=, %=) you have to worry whether left hand side and
right hand side are actually the same. But in simple cases, you often can
arrange your operator in a way that it does not matter, and in more
complex cases, the additional check should not be that expensive compared
to what else the operator does do.
  An example, where a simple assignment operator would have to worry,
would be one that contains new or delete, but where performance matters,
you don't want to have new and delete in assignment operators anyway.
  I can't think of an example, where you really want to pass a const
object as a non-const reference to a function. Maybe you can help me out?

        Cheers,
        Oliver

-- 
-- Oliver Axel Ruebenacker           Physics Graduate Student  --
-- Computational Condensed Matter Theory                       --
-- Univ. of Mass. at Amherst & Univ. of Karlsruhe, Germany     --
-- http://www.ruebenacker.de/           oliver@ruebenacker.de  --
-- 347 Prince House, UMass Amherst MA 01003    (413) 546 5919  --
-- Office: 414 Hasbrouck Lab                   (413) 545 0561  --

--------------------- 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:11 EST