A opIndexUnary quiz
Ali Çehreli
acehreli at yahoo.com
Wed Jan 2 09:52:55 PST 2013
On 01/02/2013 01:39 AM, Dmitry Olshansky wrote:
> 1/2/2013 7:52 AM, bearophile пишет:
>> 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.
I am not sure that it is an rvalue in this case. I think x is used
directly as an lvalue. Since the post-increment operator cannot be
overloaded in D, the compiler writes the previous expression as the
equivalent of the following:
{
int oldValue = data[i];
++data[i];
oldValue;
}
Since data[i].x is an lvalue int, there shouldn't be a problem.
The problem that I have seen here is that although the programmer thinks
that the post-increment operation is being defined by
opIndexUnaryRight(), opIndexUnaryRight() is not a special name. We have
seen a similar issue recently with opGet(). These names happen to look
like operator overloads but they are not.
Ali
More information about the Digitalmars-d-learn
mailing list