Thread overview
static const and user attribute
May 29, 2013
Jack Applegame
May 29, 2013
Jack Applegame
May 29, 2013
Ali Çehreli
May 29, 2013
bearophile
May 29, 2013
Dicebot
May 29, 2013
bearophile
May 29, 2013
Kenji Hara
May 29, 2013
bearophile
May 29, 2013
Which difference between enum and static const with initializer?

struct A {
    @("enum") enum int c1 = 10;
    @("const") static const int c2 = 20;
}

static assert(__traits(getAttributes, A.c1)[0] == "enum");
static assert(__traits(getAttributes, A.c2)[0] == "const"); // error

http://dpaste.dzfl.pl/7887ef90
May 29, 2013
I'm sorry. My english is terrible.
May 29, 2013
Jack Applegame:

> Which difference between enum and static const with initializer?

Currently nearly none. But it will be fixed in two or three DMD versions, and they will become const and enum.

Bye,
bearophile
May 29, 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.
May 29, 2013
Dicebot:

> I think this is a bug.

It will be fixed progressively in the next two or three DMD versions.

Bye,
bearophile
May 29, 2013
On Wednesday, 29 May 2013 at 14:50:36 UTC, bearophile wrote:
> Dicebot:
>
>> I think this is a bug.
>
> It will be fixed progressively in the next two or three DMD versions.
>
> Bye,
> bearophile

Please file it in bugzilla.

Kenji Hara
May 29, 2013
Kenji Hara:

> Please file it in bugzilla.
>
> Kenji Hara

OK, I'll add it a bit later :-)

Bye,
bearophile
May 29, 2013
On 05/29/2013 06:32 AM, Jack Applegame wrote:

> I'm sorry. My english is terrible.

Please don't say that. :) Your English is very nice and I am sure everybody here appreciates that you write in English.

Ali