about the new const regime

Dan murpsoft at hotmail.com
Wed Jan 2 10:19:34 PST 2008


Steven Schveighoffer Wrote:
> "Daniel919" wrote
> > const char[] s = "bla";
> > s = "abc" //error
> >
> > invariant char[] s = "bla";
> > s = "abc" //error
> 
> The difference is subtle.  invariant means "nothing can change this".  const 
> means "I can't change this".

The difference being that the compiler is allowed to optimize an invariant under the assumption that it won't ever *be* changed may entail caching results of evaluating it, and other similar optimizations.

Lemma 1) accepting that our algorithms are incorrect if changed by influences outside our control.

This lemma is inherent to all programs though.  One cannot control the environment on which they run.  Sure, we can perform sanity checks, but even bootloaders and OS's are run on emulators and can be influenced in unexpected ways.  I would therefore suggest that everything that is const within an entire program is therefore inherently invariant for our purposes.

Regards,
Dan



More information about the Digitalmars-d mailing list