Validity of cast(void*)size_t.max

Steven Schveighoffer schveiguy at yahoo.com
Mon Mar 5 12:41:06 UTC 2018


On 3/5/18 6:41 AM, Nordlöw wrote:
> On Wednesday, 28 February 2018 at 20:07:50 UTC, Steven Schveighoffer wrote:
>> auto x = cast(Object)((cast(size_t *)null) + 1);
> 
> Thanks, how do I store it as enum or static immutable struct member?
> 
> In
> 
> @trusted pure unittest
> {
>      class C { int value; }
>      C x;
>      C y = cast(C)((cast(size_t*)null) + 1); // indicates a lazily 
> deleted key
>      struct S
>      {
>          enum C hole1 = cast(C)((cast(size_t*)null) + 1); // TODO make work
>          static immutable C hole2 = cast(C)((cast(size_t*)null) + 1); // 
> TODO make work
>      }
> }
> 
> both `enum` and static` immutable` member declarations fail to compile 
> with the same errors:
> 
> Error: cannot perform pointer arithmetic on non-arrays at compile time
> Error: cannot perform pointer arithmetic on non-arrays at compile time
> 
> My only possible solution so far is to return the expression from an 
> inline member function which I guess cannot be optimized as good as 
> comparing to a compile-time value.

Weird, I would have expected to be able to map addresses at compile 
time, I suppose the way you could work it is:

pragma(inline, true)
C lazyDeleted() pure nothrow @trusted { return 
cast(C)((cast(size_t*)null) + 1); }

Then you can simply use this like it was an enum or immutable. It should 
go away in terms of a function call.

-Steve


More information about the Digitalmars-d-learn mailing list