Why is size_t unsigned?

monarch_dodra monarchdodra at gmail.com
Mon Jul 22 00:12:03 PDT 2013


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?


More information about the Digitalmars-d-learn mailing list