Avoiding Range Postblits
Ali Çehreli via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Wed Feb 18 15:53:53 PST 2015
On 02/18/2015 02:51 PM, "Nordlöw" wrote:
> I'm guessing that move construction plays a key role here.
move() does avoid the post-blit but you get many destructions in return:
import std.stdio;
import std.algorithm;
import core.memory;
struct Graph
{}
struct DW
{
int i;
this(int i)
{
writefln(" this() for %s", i);
this.i = i;
}
~this()
{
writefln("~this() for %s", i);
}
this(this)
{
writefln("this(this) for %s", i);
assert(false, "Oops");
}
}
DW dw(Graph gr, int i)
{
return DW(i);
}
void main()
{
auto gr = Graph();
auto arr = [ 1, 2, 3 ];
auto dws = new DW[arr.length];
foreach (i, el; arr) {
auto source = gr.dw(el);
move(source, dws[i]);
}
writeln(dws);
}
The output:
this() for 1
~this() for 0
~this() for 0
this() for 2
~this() for 0
~this() for 0
this() for 3
~this() for 0
~this() for 0
[DW(1), DW(2), DW(3)]
~this() for 3
~this() for 2
~this() for 1
Ali
More information about the Digitalmars-d-learn
mailing list