New D technique - disable usage of "new" and "delete"

jmh530 john.michael.hall at gmail.com
Mon Jan 8 01:11:56 UTC 2018


On Friday, 5 January 2018 at 20:51:14 UTC, Basile B. wrote:
>
> Yeah, i didn't explain correctly the mixin thing.
> The mixin would be used to disable "new" and "delete", e.g
>
>     enum disableNewAndDelete = "@disable new (size_t 
> size){return null;} @disable delete (void* p){}";
>
> and then you mix it in the classes of the framework that 
> doesn't want "new" and "delete" to be used:
>
>     mixin(disableNewAndDelete);

It occurs to me that you can use this to disable unsafe features 
(pointer arithmetic, casting immutability and shared most 
notably). For instance, something like below as a rough sketch (I 
didn't check that it compiles)

struct Safe(T)
{
     import std.traits : isPointer, Unqual;

     T _data;
     alias _data this;

     @disable this();

     this(T x) { _data = x; }

     @property T data()
     {
         return _data;
     }

     @property void data(T x)
     {
         _data = x;
     }

     static if (isPointer!T)
     {
         @disable U opCast(U)()
             if (isPointer!U && U != void*)
         @disable T opBinary(string op)(T rhs)
         @disable T opUnary(string op)()
         //also @disable opIndex, opDollar, opIndexUnary, 
opIndexAssign, ...
         //opIndexOpAssign
     }
     static if (isMutable!T)
     {
         @disable immutable(T) opCast(immutable(T))()
     }
     else
     {
         @disable Unqual!T opCast(Unqual!T)()
     }
     //add @disable for shared
}


More information about the Digitalmars-d mailing list