std.allocator needs your help

Jacob Carlborg doob at me.com
Mon Sep 23 23:45:40 PDT 2013


On 2013-09-23 22:32, Timon Gehr wrote:

> Some general remarks:
>
> One issue you will probably run into and maybe want to fix in some way
> during the typed allocator design is that private constructors cannot be
> called from templates parameterized on types.
>
> E.g.:
>
> module a;
>
> auto New(T,Args...)(Args args){
>      return new T(args);
> }
>
> // ---
>
> module b;
>
> class A{
>      private this(){}
> }
>
> void main(){
>      auto a = New!A(); // error
> }

Allocate the memory needed for T without using "new". Then a pointer can 
be used to bypass the protection of the constructor, something like:

extern (C) Object _d_newclass(in ClassInfo);

T New (T, Args ...) (Args args)
{
     auto object = cast(T) _d_newclass(T.classinfo);

     // use explicit type to handle overloads.
     T delegate (Args) dg = &object.__ctor;

     return dg(args);
}

-- 
/Jacob Carlborg


More information about the Digitalmars-d mailing list