size_t + ptrdiff_t

Artur Skawina art.08.09 at gmail.com
Tue Feb 21 01:03:44 PST 2012


On 02/21/12 04:12, James Miller wrote:
> If the only problem is that we need some more types, can't we add them
> in? I don't see the problem with having verbose, import-only names for
> things outside the norm, alias them if you want, longer names are
> better for newcomers. For example, what is ptrdiff_t? I have never
> seen that before, and the name doesn't help me, from the alias in
> druntime, it seems to be the type that represents the difference
> between 2 pointers. Help me out here, what on earth would I use that
> for?

In C, for storing the result of (a-b), where a and b are pointers.

> (On a side note to that, no way is ssize_t a good name, ever, forwards
> and backwards, what on earth is it? super-size_t? here to save you?
> shady-size_t? you have to make sure it doesn't sell you drugs?
> size-size_t? it has a stammer?)

It's signed, unlike the unsigned 'size_t'.


Types like ptrdiff_t are not necessary in D, because you can write portable
code using 'auto' and 'typeof()' - std C didn't have these, so a type had
to be invented for everything.

D only lacks the 'native' types; while you can, in most cases, find them out
using import and version(), the compiler obviously already knows what they
are for every supported target. So it's just about exposing them to the
compiled code in a std, defined way.

The 'C' types are only needed for interoperability, for example - the glib
bindings needed these:
> import core.stdc.limits: c_long, c_ulong;
> alias  int c_int;  // Assumes 32-bit wide int.
> alias uint c_uint; // Assumes 32-bit wide int.
> import core.sys.posix.sys.types : time_t, ssize_t;

IIRC I did not find the C int types conveniently defined anywhere and didn't
want to use GCC extensions. ssize_t is std and common enough that it could
live outside that sys.posix module. Maybe this is also true for time_t and
a few others that weren't needed in this case.

artur


More information about the Digitalmars-d mailing list