Should (p - q) be disallowed in @safe code?

Walter Bright newshound2 at digitalmars.com
Thu Jan 1 22:02:56 UTC 2026


On 1/1/2026 9:56 AM, Paul Backus wrote:
> For example, this compiles using the latest release of DMD:
> 
> ```d
> import std.stdio;
> 
> void main() @safe
> {
>      int* p = new int, q = new int;
>      writeln(q - p);
> }
> ```
> 
> [1]: https://dlang.org/spec/function.html#safe-values

Yes, that code would be safe, but it would also be garbage.

I was referring more to code like this:

char* p = &array[array.length;
for (auto q = &array[0]; p - q; ++q)
     ...


More information about the Digitalmars-d mailing list