L. David Baron wrote:
> I'm not sure what the reason to prefer size_t/ptrdiff_t vs.
> intptr_t/uintptr_t are (and I hope there aren't any cases where
> they're different).  I suppose size_t and ptrdiff_t have been around
> longer, but intptr_t and uintptr_t have clearer names.  But I also
> tend to think we should only use one set of them.

[u]intptr_t is an integer type that holds pointers.
ptrdiff_t is the integer type that holds the result of pointer arithmetic.

Because they have different meanings, the distinction between the two types to 
document the code and to (in theory) help static analysis. It may be helpful to 
ensure that the range of these types (and ssize_t/size_t) is the same (via 
static_assert) but we should still use each distinct type as appropriate.

uintptr_t x;
ptrdiff_t y;

It is expected that I can convert x contains the value of some pointer and that 
casting it to a pointer is meaningful. It does not make sense to add x to any 
pointer or any other value of type uintptr_t.

It is not expected that y contains the value of a pointer and so it is not 
expected that casting it to a pointer is meaningful. It may or may not make 
sense to add y to some particular pointer (or value of uintptr_t) or to some 
other value of type ptrdiff_t.

The compiler should (but probably doesn't) warn you when you cast the result of 
pointer arithmetic to unintptr_t, or when you cast a pointer type to ptrdiff_t, 
even if it is a reinterpret_cast.

Cheers,
Brian
_______________________________________________
dev-platform mailing list
[email protected]
https://lists.mozilla.org/listinfo/dev-platform

Reply via email to