www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Feature request: ssize_t, a signed size_t

reply =?ISO-8859-1?Q?Julio_C=E9sar_Carrascal_Urquijo?= writes:
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 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>

Thanks
Mar 04 2011
next sibling parent spir <denis.spir gmail.com> writes:
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 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
Mar 04 2011
prev sibling parent Walter Bright <newshound2 digitalmars.com> writes:
Julio CÚsar Carrascal Urquijo wrote:
 Could we have a signed size_t in Phobos? ssize_t or 
 with another name.

ptrdiff_t
Mar 04 2011