Bug 6195 – [GSoC] opSlice defined on range prevents call to postblit.
Status
RESOLVED
Resolution
DUPLICATE
Severity
major
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
Windows
Creation time
2011-06-22T08:52:00Z
Last change time
2012-04-24T18:19:32Z
Assigned to
nobody
Creator
cristi.cobzarenco
Comments
Comment #0 by cristi.cobzarenco — 2011-06-22T08:52:02Z
import std.stdio;
struct Test {
int count;
this( int x ) { count = x; }
this( this ) { writeln("Postblit!"); }
typeof(this) opSlice() { return this; }
int front() const { return 42; }
@property bool empty() const { return count == 0; }
void popFront() { --count; }
}
int main(string[] argv) {
auto v = Test(5);
foreach( x ; v )
writeln( x );
writeln( "Count: ", v.count );
return 0;
}
In the code above "Postblit!" doesn't get written, but a copy of v is definitely created since "Count: 5" gets printed. It seems that foreach() calls opSlice and for some reason the returned object gets blitted without calling the postblit ctor (maybe a misguided return value optimization?).
Removing the opSlice() definition results in correct behaviour.
Comment #1 by lovelydear — 2012-04-24T11:30:03Z
With 2.059, the output is:
PS E:\DigitalMars\dmd2\samples> rdmd bug
Postblit!
42
42
42
42
42
Count: 5
PS E:\DigitalMars\dmd2\samples>
Comment #2 by k.hara.pg — 2012-04-24T18:19:32Z
Maybe a dup of 4437.
*** This issue has been marked as a duplicate of issue 4437 ***