D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 5044 - opIndexUnary not working for post++
Summary: opIndexUnary not working for post++
Status: NEW
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: x86 Windows
: P2 major
Assignee: No Owner
URL:
Keywords: rejects-valid
: 7733 (view as issue list)
Depends on:
Blocks:
 
Reported: 2010-10-11 17:44 UTC by Austin Hastings
Modified: 2024-12-13 17:53 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Austin Hastings 2010-10-11 17:44:11 UTC
This code:
==========
module scratch;

class C( element_t ) {

	element_t [] contents;

	element_t opIndex( int index ) {
		return contents[ index ];
	}
	
	element_t opIndexUnary( string op )( int index ) {
		
		static if( op == "++" ) {
			return ++contents[ index ];
		}
		else {
			static assert( false );
		}
	}
	
}

unittest {
	auto c = new C!int();
	
	c[0]++;
}
==========
Produces this output:
==========
$ dmd -c -unittest scratch.d
scratch.d(42): Error: c.opIndex(0) is not an lvalue
==========
But if the penultimate line: "c[0]++" is replaced by "++c[0]", there is no problem.

And finally, if the opIndex is changed to return a 'ref', there is not problem, but the opIndexUnary operator is not called - the reference is incremented as an int, I think.
Comment 1 monarchdodra 2012-07-26 01:28:56 UTC
*** Issue 7733 has been marked as a duplicate of this issue. ***
Comment 2 dlangBugzillaToGithub 2024-12-13 17:53:56 UTC
THIS ISSUE HAS BEEN MOVED TO GITHUB

https://github.com/dlang/dmd/issues/17518

DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB