Bug 7733 – [tdpl] opIndexUnary not called with postfix increment/decrement operators
Status
RESOLVED
Resolution
DUPLICATE
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2012-03-19T10:56:00Z
Last change time
2012-07-26T01:28:53Z
Keywords
TDPL
Assigned to
nobody
Creator
hsteoh
Comments
Comment #0 by hsteoh — 2012-03-19T10:56:39Z
struct S {
int[] data;
void opIndex(size_t i) { return data[i]; }
void opIndexUnary(string op)(size_t i) {
return mixin(op ~ "data[i]");
}
}
void main() {
S s;
s.data = [1,2,3];
++s[0]; // OK
s[0]++; // Error: s.opIndex(1u) is not an lvalue
}
This contradicts the specs, that specify that x++ is translated to ++x, which should cause opIndexUnary!"++" to be called, not opIndex.
Comment #1 by hsteoh — 2012-03-19T10:57:58Z
Oops, typo in previous post: should be int opIndex(size_t i), not void.
Comment #2 by hsteoh — 2012-03-20T06:50:03Z
TDPL references:
p.378: "Postincrement and postdecrement are generated automatically from preincrement and predecrement, as described in ยง 12.2.2 on page 369.
p.369: describes the rewriting of x++ as ++x when the value is not needed, and as ((ref x){auto t=x; ++x; return t;})(a) when the value is needed.
Comment #3 by monarchdodra — 2012-07-26T01:28:53Z
*** This issue has been marked as a duplicate of issue 5044 ***