June 12, 2020 Should opIndex take precedence over pointer indexing? | ||||
---|---|---|---|---|
| ||||
struct Foo { float* what; float opIndex(size_t idx) { return what[idx]; } } Foo* foo; float x = foo[idx]; // *** *** <source>(68): Error: cannot implicitly convert expression `foo[cast(ulong)idx]` of type `Foo` to `float` IE, pointer indexing of 'foo' takes precedence over the opIndex of foo. Is this expected? |
June 12, 2020 Re: Should opIndex take precedence over pointer indexing? | ||||
---|---|---|---|---|
| ||||
Posted in reply to claptrap | On Friday, June 12, 2020 5:12:35 PM MDT claptrap via Digitalmars-d-learn wrote:
> struct Foo
> {
> float* what;
> float opIndex(size_t idx) { return what[idx]; }
> }
>
> Foo* foo;
> float x = foo[idx]; // ***
>
>
> *** <source>(68): Error: cannot implicitly convert expression
> `foo[cast(ulong)idx]` of type `Foo` to `float`
>
> IE, pointer indexing of 'foo' takes precedence over the opIndex of foo. Is this expected?
Yes, it's expected behavior. . is the only operator which implicitly dereferences a pointer.
- Jonathan M Davis
|
Copyright © 1999-2021 by the D Language Foundation