Should (p - q) be disallowed in @safe code?
claptrap
clap at trap.com
Thu Jan 1 15:16:48 UTC 2026
On Thursday, 1 January 2026 at 06:15:09 UTC, Walter Bright wrote:
> Consider:
> ```d
> @safe
> size_t distance(int* p, int* q) => p - q;
> ```
> The difficulty here is when p and q may not be pointing into
> the same memory object. If they're not, the result is nonsense:
I use this all the time to iterate multiple arrays in lockstep.
size_t offset = q-p;
you access q with "p[offset]", and you just iterate p
I tried to avoid using it but it is just faster sometimes,
More information about the Digitalmars-d
mailing list