const/invariant

guslay guslay at gmail.com
Wed Dec 5 10:40:03 PST 2007


> 
> class C {}
> 
> what's the difference between:
> 
> const C c = new C;
> and
> invariant C c = new C;
> 


I don't see any difference between const and invariant when they are constructed locally, like in your example. The compiler know they can't change.

There is a difference however when they are passed as argument to a function.

void foo( const/invariant int* x ) {...}

In the const case, the value refered by x can be modified between the beginning and the end of the function. Not directly, but indirectly, either by aliasing (see http://www.digitalmars.com/d/const3.html) or in maybe if x is shared between threads.

Invariant are guaranteed to never change, directly or indirectly.

It's my understanding. I hope i'm not spreading too much wrongness.



More information about the Digitalmars-d-learn mailing list