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

Paul Backus snarwin at gmail.com
Sat Feb 27 13:32:24 UTC 2021


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.


More information about the Digitalmars-d mailing list