[phobos] What does this code do?

Andrei Alexandrescu andrei at erdani.com
Tue Aug 17 11:07:18 PDT 2010


The idea is to create a typed object in uninitialized memory. Generally 
using the assignment operator for that is suicide because that guy 
assumes the object was previously initialized.

Instead of placement new, a better way is to use emplace(). Grep for it 
to see documentation and examples. Please let us know if you hit another 
hitch.


Andrei

On 08/16/2010 07:01 PM, David Simcha wrote:
> I'm trying to revamp array() to follow Bearophile's very good suggestion
> of letting any foreachable object be convertible to an array even if
> it's opApply-based, not a range. My efforts have been blocked by this
> cryptic piece of code:
>
> foreach (ref e; result)
> {
> // hacky
> static if (is(typeof(&e.opAssign)))
> {
> // this should be in-place construction
> new(&e) E(r.front);
> }
> else
> {
> e = r.front;
> }
> r.popFront;
> }
>
> It took me awhile to even wrap my head around what the point of that
> code is. First of all, why are we bypassing opAssign? Secondly, what if
> the E doesn't have a constructor that takes only another E as an argument?
> _______________________________________________
> phobos mailing list
> phobos at puremagic.com
> http://lists.puremagic.com/mailman/listinfo/phobos


More information about the phobos mailing list