Why does this not compile?

Simen Kjærås simen.kjaras at gmail.com
Tue Mar 6 13:42:55 UTC 2018


On Tuesday, 6 March 2018 at 12:00:43 UTC, Steven Schveighoffer 
wrote:
> On 3/6/18 6:21 AM, Simen Kjærås wrote:
>> On Tuesday, 6 March 2018 at 10:03:54 UTC, Shachar Shemesh 
>> wrote:
>>> void main() {
>>>     struct S {
>>>         uint value;
>>>
>>>         ~this() {
>>>         }
>>>     }
>>>
>>>     const S a = S(12);
>>>     S b = a;
>>> }
>>>
>>> test.d(10): Error: cannot implicitly convert expression a of 
>>> type const(S) to S
>> 
>> Looks like a bug to me - please file one in bugzilla.
>
> Nope. It's not a bug. S contains a pointer, namely the context 
> pointer for main.

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);
}

--
   Simen


More information about the Digitalmars-d mailing list