Discussion Thread: DIP 1035-- at system Variables--Community Review Round 2

Steven Schveighoffer schveiguy at gmail.com
Sat Feb 27 15:21:41 UTC 2021


On 2/27/21 8:32 AM, Paul Backus wrote:
> On Saturday, 27 February 2021 at 08:23:38 UTC, Kagamin wrote:
>> A simple workaround is an unsafe wrapper:
>>
>> struct Unsafe
>> {
>>     private T a;
>>     T get() @system { return a; }
>> }
>>
>> struct IntSlice
>> {
>>     private Unsafe!(int*) ptr;
>>     private Unsafe!size_t length;
>> ...
> 
> The compiler will still allow you to 
> void-initialize/overlap/reinterpret-cast an Unsafe!size_t in @safe code, 
> because it considers size_t to be a safe type.
> 
> You *can* get the compiler to treat any value as unsafe by overlapping 
> it with a pointer; e.g.,
> 
> struct Unsafe(T)
> {
>      union Storage { T value; void* dummy; }
>      Storage storage;
>      ref T get() { return Storage.value; }
>      /* ... */
> }
> 
> But this introduces memory overhead for any T smaller than a pointer, 
> which is not ideal.

Better mark that get as @system, or it can be used in @safe code.

-Steve


More information about the Digitalmars-d mailing list