[Issue 14470] New: Reuse of object memory: new emplace overload

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Mon Apr 20 00:12:30 PDT 2015


https://issues.dlang.org/show_bug.cgi?id=14470

          Issue ID: 14470
           Summary: Reuse of object memory: new emplace overload
           Product: D
           Version: unspecified
          Hardware: All
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: Phobos
          Assignee: nobody at puremagic.com
          Reporter: rswhite4 at gmail.com

As mentioned here:
http://forum.dlang.org/thread/wucqfklowovtyhpafjkz@forum.dlang.org#post-ydiprvqyjrqgeahqdbwl:40forum.dlang.org
it would be nice if I could reuse object memory easily. Therefore std.conv
should add an emplace overload which takes an object:

----
T emplace(T, Args...)(ref T obj, auto ref Args args) if (is(T == 
class)) {
    if (obj is null)
        return null;

    enum size_t ClassSize = __traits(classInstanceSize, T);
    void[] buf = (cast(void*) obj)[0 .. ClassSize];

    import std.conv : emplace;
    return emplace!(T)(buf, args);
}
----

It would be also nice, if D could add placement-new like C++:
----
Foo f = new Foo(42);
new (f) Foo(23);
----

It looks way more cleaner and since D is aiming to be a system language,
reusing memory is a naturally behaviour.

--


More information about the Digitalmars-d-bugs mailing list