Manifest constant class instances

kinke noone at nowhere.com
Sun Nov 4 21:08:56 UTC 2018


On Sunday, 4 November 2018 at 20:57:26 UTC, lngns wrote:
> On Sunday, 4 November 2018 at 20:50:45 UTC, kinke wrote:
>> On Sunday, 4 November 2018 at 19:28:14 UTC, lngns wrote:
>>> On Sunday, 4 November 2018 at 19:20:53 UTC, kinke wrote:
>>>> On Sunday, 4 November 2018 at 19:02:33 UTC, lngns wrote:
>>>>> I can understand for pointers to struct, as the pointer 
>>>>> will be invalid at runtime, but, unless I am mistaken, 
>>>>> classes are not concerned by pointer semantics.
>>>>
>>>> A class reference is a pointer too, so using it at runtime 
>>>> would be invalid too.
>>>
>>> Yes but given it works with static constants I would assume 
>>> the compiler already abstracts away this point. Otherwise 
>>> there would be a mismatch between what the compiler allocates 
>>> and what the runtime allocates.
>>> Am I wrong?
>>
>> The instance for a static class reference lives in the data 
>> segment of the binary and is not GC-allocated.
>
> Yes this was actually my point.
> Both the compiler and the program refer to the same object, so 
> the pointer is necessarily valid at runtime.

`enum c = new C()` doesn't imply that the instance lives at 
runtime too. `static const c = new C()` on the other hand does. 
This should arguably work though, but doesn't:

```
class C {}
static immutable c = new C();
enum p = c;

void main()
{
     import std.stdio;
     writeln(p);
}
```


More information about the Digitalmars-d mailing list