Why does this not compile?

Shachar Shemesh shachar at weka.io
Tue Mar 6 14:42:42 UTC 2018


On 06/03/18 16:06, Steven Schveighoffer wrote:
> On 3/6/18 8:56 AM, Steven Schveighoffer wrote:
>> So a bug report is in order. It should be decided one way or another 
>> -- either the context pointer is part of the struct type or it isn't.
> 
> There is a third possibility:
> 
> It's part of the type AND it's typed as const if it can be (i.e. none of 
> the methods in the struct modify the context data, and any use of the 
> context data implicitly casts from const).
> 
> -Steve

For what it's worth, I vote for option #2 (it isn't const even if the 
struct is), for the same reason that const structs can modify static 
variables - it's not part of the struct.

I'll phrase is another way, if this shouldn't compile:

unittest {
   int i;

   struct S {
     int a;

     void func() const {
       ++i;
     }
   }
}

Then neither should this:
struct S {
   static int i;
   int a;

   void func() const {
     ++i;
   }
}

I fail to see any reasoning[1] that disallows the former but allows the 
later.

Shachar

1 - That is obviously not true. I see the reasoning all too well. Static 
vars are like globals, and you're not getting to them via the struct's 
instance. I would argue that, as far as the programmer, however, there 
is no difference. There is no semantic difference between allowing the 
first and not the second.


More information about the Digitalmars-d mailing list