non-instance accessibility of immutable instance variables with initializers

Simen Kjaeraas simen.kjaras at gmail.com
Sun Jun 3 08:31:24 PDT 2012


On Sun, 03 Jun 2012 15:40:32 +0200, Timon Gehr <timon.gehr at gmx.ch> wrote:

> DMD 2.059:
>
> struct S{
>      immutable x = [1];
>      immutable y = 1;
> }
>
> void main(){
>      writeln(S.x);        // ok
>      writeln(&S.x);       // ok
>      writeln(S.y);        // ok
>      // writeln(&S.y);    // error
>      with(S) writeln(&y); // ok (but resulting pointer is wrong)
> }
>
> This behaviour is obviously buggy, but I am not sure to what extent.
>
> What is the intended behaviour? Should initialised immutable instance
> variables be accessible without an instance at all?


It gets worse:

     writeln(S.sizeof);  // 1, which is the same as an empty struct
     S s;
     writeln(&s);        // Gives a good pointer
     writeln(&s.x);      // Gives a completely different pointer
                         // (the same as for &S.x)

This should show clearly that the compiler treats these as enum instead of
immutable, and thus do not leave them in the struct.


More information about the Digitalmars-d mailing list