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

--- Comment #11 from Martin Nowak <[email protected]> ---
Here is how you define a properly typed insertBack method.

struct Buffer(T) // T can be const/immutable
{
  // value type, requires insertBack(move(val)) for non-copyable types
  // compiler will perform any implicit conversions
  void insertBack(T value)
  {
    reserve(1);
    memcpy(ptr + idx, &value, T.sizeof);

    // clear value, so it's destructor won't double free anything
    static if (hasElaborateDestructor!T)
    {
      static if (!hasElaborateAssign!T && isAssignable!T)
        chunk = T.init;
      else
      {
        import core.stdc.string : memcpy;
        static immutable T init = T.init;
        memcpy(&value, &init, T.sizeof);
      }
    }
  }
}

--

Reply via email to