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

jmh530 john.michael.hall at gmail.com
Fri Jan 5 20:45:20 UTC 2018


On Friday, 5 January 2018 at 19:44:38 UTC, Basile B. wrote:
> [snip]
> - Libraries that don't want their aggregates to reside on the 
> GC heap can use this, with a mixin. (note: although the 
> GCAllocator can still be used...)

Cool.

Not sure I'm following with your point with the mixin. A mixin 
like below works fine, but it would be a little annoying to get 
the constructors working. The only way I can think of is to pass 
them as strings to the create function.

import std.experimental.allocator;
import std.experimental.allocator.mallocator;

class Foo
{
     int a;
     @disable new (size_t size){return null;}
     @disable delete (void* p){}
}

string create(T, string name)()
{
     import std.conv : to;

     return T.stringof ~ " " ~ name ~ " = make!" ~ T.stringof ~
                                               
"(Mallocator.instance);\n" ~
         "scope(exit) dispose(Mallocator.instance, " ~ name ~ ");";
}

void main()
{
     mixin(create!(Foo, "f"));
     f.a = 1;
}


More information about the Digitalmars-d mailing list