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

Timon Gehr timon.gehr at gmx.ch
Thu Jan 1 15:18:59 UTC 2026


On 1/1/26 10:10, Vladimir Panteleev wrote:
> On Thursday, 1 January 2026 at 06:15:09 UTC, Walter Bright wrote:
>> Thoughts?
> 
> I don't think so. An expression which calculates a `size_t` (or 
> `ptrdiff_t`) value without side effects is memory-safe.
> 
> What you do with the index (valid or not) would be scrutinized by the 
> usual rules.
> 

In C, subtracting pointers to different memory objects is undefined 
behavior, hence side-effecting.

Subtracting pointers can be `@safe` iff it is always defined behavior. 
(Even if the defined behavior is to yield a nonsense value.)

I am not sure how GDC and LDC are currently treating this.


More information about the Digitalmars-d mailing list