static const and user attribute

Dicebot m.strashun at gmail.com
Wed May 29 07:19:15 PDT 2013


On Wednesday, 29 May 2013 at 13:30:12 UTC, Jack Applegame wrote:
> Which difference between enum and static const with initializer?

enum is pure compile-time entity. It does not take space or have 
a location, it is just a named value.
"static const x = 42" is a thread-local variable which is 
initialized to 42 and won't change. You still can take its 
address and do anything you can do with normal const variable.

Well, that is how it is supposed to be. In practice, looks like 
DMD does some weird optimization for const value types.

Global module scope, 2.063:

@(42) int x1 = 42;
@(42) const int x2 = 42;
@(42) const int x3;

pragma(msg, __traits(getAttributes, x1)); // works
pragma(msg, __traits(getAttributes, x2)); // fails
pragma(msg, __traits(getAttributes, x3)); // works

I think this is a bug.


More information about the Digitalmars-d-learn mailing list