Why is size_t unsigned?

JS js.mdnq at gmail.com
Mon Jul 22 02:34:34 PDT 2013


On Monday, 22 July 2013 at 07:12:07 UTC, monarch_dodra wrote:
> On Monday, 22 July 2013 at 03:47:36 UTC, JS wrote:
>> Doing simple stuff like
>>
>> for(int i = 0; i < s.length - 1; i++) fails catastrophically 
>> if s is empty. To make right one has to reduce performance by 
>> writing extra checks.
>
> Not really, you could instead just write your loop correctly.
> 1. Don't loop on int, you are handling a size_t.
> 2. Avoid substractions when handling unsigned.
>
> for(size_t i = 0; i + 1 < s.length; i++)
>
> Problem solved?

Oh sure... problem solved... rriiiighhhtt.....

how about s[i - 1..n]?

You going to go throw some ifs around the statement that uses 
that? Use a ternary if? So I'm forced to use a longer more 
verbose method, and also introduce bugs, because the most 
obvious, simplest, and logical solution, s[max(0, i-1)..n] won't 
work.


More information about the Digitalmars-d-learn mailing list