Constructing a class in-place
Shachar Shemesh
shachar at weka.io
Wed Jul 25 08:05:28 UTC 2018
Forget the "why" for the moment.
T construct(T, ARGS...)(ARGS args) if( is(T==class) ) {
auto buffer = new ubyte[__traits(classInstanceSize, T)];
T cls = cast(T)buffer.ptr;
// Is this really the best way to do this?
buffer[] = cast(ubyte[])typeid(T).initializer()[];
cls.__ctor(args);
return cls;
}
My question is this: Is this the correct way to do it? There are steps
here that seem kinda arbitrary, to say the least.
I am looking for something akin to C++'s placement new.
Thank you,
Shachar
More information about the Digitalmars-d
mailing list