If you've never heard of automem before, I wrote it to have C++-style smart pointers in D that I could use in @nogc code:


I needed something like a std::vector that can be @nogc so I wrote it. I wondered whether or not to put it in a collections package or not, then punted on the decision and just stuck it in automem. Anyway, code:

// *no* GC allocations below if theAllocator is set to an allocator that
    // never allocates on the GC heap

    import automem.vector;  // there's an `array` alias too
    auto v = vector(0, 1, 2);
    v ~= 3;
    v ~= only(4, 5);
    assert(v[] == [0, 1, 2, 3, 4, 5]);

If you want the @nogc compile-time guarantee, specify an allocator:

    () @nogc {
        auto v = vector!Mallocator(0, 1, 2, 3);
        v ~= only(4, 5, 6);
        // etc.

Enjoy (hopefully)!

Reply via email to