const & invariant at global scope

torhu fake at address.dude
Mon Jul 9 08:53:46 PDT 2007


Steve Teale wrote:
> At global scope, is there any difference between
> 
> const int n = 42;
> invariant int n = 42;
> 
> If not, which is the preferred style in translated C header files and such?
> 

If I'm not mistaken, they are more or less the same.  const has the 
advantage of being 1.x compatible.  invariant has the advantage of 
possibly being optimized away, and not take up storage space.  But the 
current implementation stores invariants.  I'm not sure if consts will 
always have to be stored or not, but currently they are.

'final' variables are guaranteed to take up space, and don't work with D 
1.0.

The best way if you've got more than a few constants, would be to use 
enums.  enums don't take up any space.  If you don't want to use enums, 
I'd use consts for now.



More information about the Digitalmars-d mailing list