D-Programming-Language/phobos — pull request #1082 by monarchdodra (2013-01-21 16:23:36+00:00)
Title: Fix emplace

Because it's been broken for *months*, with no improvements in sight. Having a correct `emplace` is (IMO) *critical*. I'm opening this again (3rd time)

I wrote this fix, which is
* small and relatively simple (as simple as emplace gets)
* stand alone (no dependency on other pulls)
* efficient
* complete (AFAIK)

Fixes:
* correct `emplace(void)` implementation for static arrays.
* unittest `emplace(args)` for static arrays.
* emplace never calls opAssign.
* emplace choses postblit over constructor (when both are possible).
* Correctly postblits and/or constructs nested members...
* ...but refuses to call disabled postblits.
* Correctly handles emplacement from alias this.
* Correctly refuses to modify const data
* Supports bug 8847.
* Correctly refuses to posblit from an immutable, when implicit cast is not possible.

...phew ! And unittests for all of that, of course. Did I miss anything?

Also, can work in safe and nothrow code! And sometimes CTFE to boot (yay).

Has a deprecated branch for calling opCall: arguably, opCall is not construction, so it should not be supported. However, the old implementation allowed opCall, so I added a deprecated branch.

Finally: ALWAYS diagnoses illegal args at top level with a verbose explanation for special cases (EG, no internal errors).

In regards to context pointer, it is copied when available. Otherwise, emplace will initialize it to null. This is kind of ugly, but it consistent behavior with dealing with voldemort types (such as when they are constructed inside any template).

--------
PLEASE PLEASE PLEASE take the time to review this.

This isn't some small bug fix, or some petty performance improvement. This *needs* to make it into phobos.

There may be ways to optimize certain branches (thinking static array default emplacement), but correctness trumps efficiency at this point...

If you have any doubts, please voice them, as much as you can. I can justify the behavior (or lack thereof) of everything in this pull.