Feature request: ssize_t, a signed size_t
spir
denis.spir at gmail.com
Fri Mar 4 13:51:48 PST 2011
On 03/04/2011 09:54 PM, Julio César Carrascal Urquijo wrote:
> I was writing something like the following code:
>
> // double y1, y2, width;
> size_t index = to!size_t((x2 - x1) / width);
> if (index >= 0 && index < grid.length) {
> ...
> } else {
> ...
> }
>
> When I finished writing the condition I remembered that size_t is actually an
> uint so it can't be negative.
>
> Just for fun I tested this:
>
> writeln(to!size_t(-1));
>
> std.conv.ConvOverflowException at std\conv.d(8): Error: overflow Conversion
> negative overflow
>
> That's not what I was expecting but it's OK. For my use case I changed the code
> use int but, with 64bit already here, that's not quite correct.
>
> So if I may suggest: Could we have a signed size_t in Phobos? ssize_t or with
> another name.
>
> I don't want to replace size_t. I just want a signed type that's correct for
> storing an index for an array in 32 and 64 bit.
>
> PS: I realize this has been discussed before. I just hope we get some luck this
> time. <g>
unittest {
size_t i;
sizediff_t n;
writefln("%s %s", typeid(i), typeid(n));
// --> "uint int" on 32-bits
}
Denis
--
_________________
vita es estrany
spir.wikidot.com
More information about the Digitalmars-d
mailing list