A opIndexUnary quiz
Dmitry Olshansky
dmitry.olsh at gmail.com
Wed Jan 2 01:39:40 PST 2013
1/2/2013 7:52 AM, bearophile пишет:
> Era Scarecrow:
>
>> Well I see that you have opIndexUnary twice; According to the manual
>> you wouldn't need as it would rewrite the code so you only need it once;
>
> And as you have seen if you remove the useles opIndexRight the program
> keeps compiling with no errors and keeps asserting at run-time:
>
>
>
> struct Foo {
> int x;
> alias this = x;
Implicit conversion to int...
> }
>
> class Bar {
> Foo[] data;
>
> this() {
> data.length = 10;
> }
>
> Foo opIndex(uint i) {
> return data[i];
> }
>
> void opIndexUnary(string op)(uint i) if (op == "++") {
> data[i]++;
...and ++ somehow works with rvalue.
The fact that it's allowed is dangerous if you ask me.
> }
> }
>
> void main() {
> auto barfoo = new Bar;
> ++barfoo[3];
> assert(barfoo.data[3] == 1);
> barfoo[3]++;
> assert(barfoo.data[3] == 2);
> }
>
>
> Bye,
> bearophile
--
Dmitry Olshansky
More information about the Digitalmars-d-learn
mailing list