Manifest constant class instances

lngns contact at lngnslnvsk.net
Sun Nov 4 20:53:34 UTC 2018


On Sunday, 4 November 2018 at 20:26:13 UTC, Alex 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 difference is, that a manifest constant does not possess an 
> address, while a static const does, see e.g.,
> https://p0nce.github.io/d-idioms/#Precomputed-tables-at-compile-time-through-CTFE
>
> And if you new an instance, then, you get a pointer, which 
> collides with the way of working of an enum, which is meant to 
> be addressless.

I do not see how it collides.
This compiles fine:

static const int i = 42;
enum const(int)* ip = &i;

void main()
{
     import std.stdio;
     writeln(*ip); //42
}

An enum can be a pointer.
Plus, given the compiler already applies magic with static const 
classes by making both the pointer and the actual object static 
and constant, instead of only the pointer, it looks to me this is 
a pure implementation detail.


More information about the Digitalmars-d mailing list