Why does this not compile?

Steven Schveighoffer schveiguy at yahoo.com
Tue Mar 6 14:06:06 UTC 2018


On 3/6/18 8:56 AM, Steven Schveighoffer wrote:
> On 3/6/18 8:42 AM, Simen Kjærås wrote:
> 
>> It's a bug. As pointed out elsewhere in this thread, it compiles 
>> correctly when there's no destructor. Essentially, this bug is caused 
>> by the context pointer being typed as void*, and becoming (of course) 
>> const(void*) for a const(S). If it'd been const(void)* in the first 
>> place, Shachar's code would have compiled and worked correctly.
>>
>> Is it misleading for the context pointer to be const(void)*? In a way, 
>> maybe. However, it's opaquely typed, and its constness says nothing 
>> about what's on the other end. Also, the language completely 
>> disregards the constness in any case:
>>
>> unittest {
>>      int i = 0;
>>      struct S {
>>          int n;
>>          void fun() const {
>>              i++;
>>          }
>>      }
>>      const S s;
>>      assert(i == 0);
>>      s.fun();
>>      assert(i == 1);
>> }
> 
> That, I would consider a bug. If it's not, then definitely, you should 
> be able to implicitly cast to/from const.
> 
> 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


More information about the Digitalmars-d mailing list