Private enum members + Descent
Lars T. Kyllingstad
public at kyllingen.NOSPAMnet
Tue Oct 27 07:39:48 PDT 2009
Yigal Chripun wrote:
> Lars T. Kyllingstad Wrote:
>
>> I was going to protest: "But what about const struct members? Should the
>> size of the following struct depend on whether the compiler determines
>> it can optimize by turning its const member into a manifest constant?"
>>
>> struct Foo { const int bar = 123; }
>>
>> But then I decided to actually try it with the current DMD2, and found
>> that the compiler does just that!
>>
>> writeln(Foo.sizeof); // Prints "1", not "4"
>>
>> Foo foo;
>> auto p = &foo.bar; // Error: constant 123 is not an lvalue
>>
>> What's going on? Is this intended behaviour?
>>
>> -Lars
>
> what D version?
DMD 2.035.
> in D1 const is a manifest constant as far as I know.
Yes, but in D2 the spec says that const is a constant view of mutable
data. There isn't (or at least I can't find) mention that struct members
are excepted from this rule.
> to answer to general question, "what about structs":
> I'd say that the same rules should apply just like inside functions.
>
> struct Foo { const int num = 42; } // num should be a manifest const
>
> can you give a use-case/example where you wouldn't want to do that but rather store the constant in the struct?
In any case where a different view of the same data is non-const. Or
would you also disallow casting away const-ness?
-Lars
More information about the Digitalmars-d
mailing list