D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 6195 - [GSoC] opSlice defined on range prevents call to postblit.
Summary: [GSoC] opSlice defined on range prevents call to postblit.
Status: RESOLVED DUPLICATE of issue 4437
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All Windows
: P2 major
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-06-22 08:52 UTC by Cristi Cobzarenco
Modified: 2012-04-24 18:19 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Cristi Cobzarenco 2011-06-22 08:52:02 UTC
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 SomeDude 2012-04-24 11:30:03 UTC
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 Kenji Hara 2012-04-24 18:19:32 UTC
Maybe a dup of 4437.

*** This issue has been marked as a duplicate of issue 4437 ***