D needs emplacement new
Namespace via Digitalmars-d
digitalmars-d at puremagic.com
Sat Apr 25 12:09:14 PDT 2015
On Saturday, 25 April 2015 at 18:57:13 UTC, Adam D. Ruppe wrote:
> On Saturday, 25 April 2015 at 18:50:59 UTC, Namespace wrote:
>> Nice name, fits better. But that should be in phobos, don't
>> you think?
>
> meh, it might be nice to have, but not being in phobos isn't a
> big deal to me for little utilities like this
I'm sure newcomers (especially those coming from C++) would be
delighted if they had the possibility to reuse their memory. In
addition, it could be impossible for them to write such "hack"
itself.
I already hear the cries: "D wasted memory" and: "I can not reuse
my memory".
And then it would be less attractive for such people, to switch
to D.
I would suggest something like:
----
T emplace(T, U...)(ref T obj, auto ref U args) if (is(T ==
class)) {
if (!obj)
return null;
import std.conv : emplace; // temporary, as long it is not in
std.conv
enum ClassSizeOf = __traits(classInstanceSize, T);
void[] buf = (cast(void*) obj)[0 .. ClassSizeOf];
return emplace!(T)(buf, args);
}
T emplaceOrNew(T, U...)(ref T obj, auto ref U args) if (is(T ==
class)) {
if (!obj)
return new T(args);
return emplace(obj, args);
}
----
More information about the Digitalmars-d
mailing list