Implicit dereferencing
"Luís
"Luís
Mon Apr 1 17:07:30 PDT 2013
Consider this (non-portable) code:
{
int[2] a;
int[2] b;
int[2] *c;
c = &a;
c[0] = 7;
assert(a[0] == 7); // OK, as expected
c[1] = 42;
assert(b[0] == 42); // do we really want this semantics?
}
Because indexing c automatically dereferences the pointer, "c[0]
= 7" assigns to a[0], as expected. This is the same as "(*c)[0] =
7", which is useful.
But "c[1] = 42" is equivalent to "(*(c+1)) = 42", which might not
be obvious. In this case it ends writing to b[0]. Why not make
the semantics be ((*c)+1) = 42, instead? Wouldn't that be more
useful?
Also, why don't we have pointer dereferencing for AAs?
{
struct Foo { int f; }
// given that this works...
Foo x;
Foo *y = &x;
x.f = 1;
y.f = 2; // implicit dereferencing
// coudln't this be made to work?...
int[string] c;
int[string] *d;
d = &c;
c["foo"] = 1;
d["foo"] = 2; // fails, dereferencing must be explicit
(*d)["foo"] = 2; // works, of course
}
More information about the Digitalmars-d
mailing list