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 by monarchdodra — 2012-07-26T01:28:56Z
*** Issue 7733 has been marked as a duplicate of this issue. ***
Comment #2 by robert.schadek — 2024-12-13T17:53:56Z