emplace, scope, enforce [Was: Re: Manual...]

bearophile bearophileHUGS at lycos.com
Wed Jul 21 03:06:56 PDT 2010


Rory McGuire:
> Takes 18m27.720s in PHP :)

You have lot of patience :-)


> can't test D version I don't have emplace and dsource is ignoring me.

This was Andrei's code before dsource went down (ddoc and unittest removed):

T emplace(T, Args...)(void[] chunk, Args args) if (is(T == class)) {
    enforce(chunk.length >= __traits(classInstanceSize, T));
    auto a = cast(size_t) chunk.ptr;
    enforce(a % real.alignof == 0);
    auto result = cast(typeof(return)) chunk.ptr;

    // Initialize the object in its pre-ctor state
    (cast(byte[]) chunk)[] = typeid(T).init[];

    // Call the ctor if any
    static if (is(typeof(result.__ctor(args))))
    {
        // T defines a genuine constructor accepting args
        // Go the classic route: write .init first, then call ctor
        result.__ctor(args);
    }
    else
    {
        static assert(args.length == 0 && !is(typeof(&T.__ctor)),
                "Don't know how to initialize an object of type "
                ~ T.stringof ~ " with arguments " ~ Args.stringof);
    }
    return result;
}

Bye,
bearophile


More information about the Digitalmars-d mailing list