opIndexUnary post in-/decrement how to ?
vit
vit at vit.vit
Wed Jul 14 14:39:03 UTC 2021
On Wednesday, 14 July 2021 at 13:16:49 UTC, Tejas wrote:
> On Wednesday, 14 July 2021 at 13:09:56 UTC, vit wrote:
>> On Wednesday, 14 July 2021 at 12:49:58 UTC, Tejas wrote:
>>> [...]
>>
>> From doc: https://dlang.org/spec/operatoroverloading.html
>> Postincrement e++ and Postdecrement e-- Operators
>> These are not directly overloadable, but instead are rewritten
>> in terms of the ++e and --e prefix operators:
>>
>> Postfix Operator Rewrites
>> op rewrite
>> e-- (auto t = e, --e, t)
>> e++ (auto t = e, ++e, t)
>>
>>
>> Rewriting part doesn't work with operator overloading.
>
> If the rewriting part doesn't work with overloading then why
> aren't we allowed to explicitly overload
> ```post-increment/decrement``` operators?
>
> How else will the OP solve their problem?
>
> It must rewrite, otherwise it's impossible (I think).
This work:
```d
import std.stdio;
struct abc{
int[100] a;
ref int opIndex(int index)return{
return a[index];
}
}
void main (){
abc s;
s[0]++;
++s[0];
}
```
More information about the Digitalmars-d-learn
mailing list