www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - const & invariant at global scope

reply Steve Teale <steve.teale britseyeview.com> writes:
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?
Jul 06 2007
parent torhu <fake address.dude> writes:
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.
Jul 09 2007