New D technique - disable usage of "new" and "delete"
Basile B.
b2.temp at gmx.com
Fri Jan 5 20:51:14 UTC 2018
On Friday, 5 January 2018 at 20:45:20 UTC, jmh530 wrote:
> 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;
> }
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);
More information about the Digitalmars-d
mailing list