4 ms·
+1. Everytime I see for(int i=0;...;i++) I wonder why we have developed this habit of defaulting all int as signed and consider uint as taboo (most coding guide
by sytelus 12y ago
+1. Everytime I see for(int i=0;...;i++) I wonder why we have developed this habit of defaulting all int as signed and consider uint as taboo (most coding guidelines asks not to use them unless "you know what you are doing"). Most of the time we use integers for counting and so uint should have been more natural. I did this in one of my libraries I was writing from scratch and I was happy for a while but then I got in to trouble because there is lot of code out there with interfaces expecting signed ints even though they should using uint. So ultimately the legacy forced me back to default again at using signed int.
- dkbrk 12y ago> Everytime I see for(int i=0;...;i++) In this case, it makes absolutely no difference at all. It could be argued that writing unsigned int would make the code slightly harder to read. That said, I like to use stdint.h and unint32_t would, I think, not have any drawbacks. > there is lot of code out there with interfaces expecting signed ints even though they should using uint That's not a good reason to not use unsigned integers, it's a zero-overhead cast from unsigned to signed (at the risk of overflowing into the negative).
- Joky 12y agoIt change the semantic on the loop bound, and thus what the compiler can/cannot do when optimizing the code. Using uint limits the optimizer...
- detrino 12y agoUsing int also limits the optimizer in some ways, for example division/modulo becomes more expensive. Every time I see someone mention the optimization argument for signed integers I ask for examples and I've yet see a good one.
- yongjik 12y agoWell, in my case, from time to time I have to do these stuff: for (int i = x.size() - 1; i >= 0; i--) ... for (int i = 0; i < x.size() - 1; i++) if (x[i] < x[i+1]) ... Both will blow up badly with unsigned ints. (Well, to be fair, both will blow up with signed ints if x.size() is greater than 2G, so it's a matter of expectations.)
- Aldo_MX 12y agofor( size_t i = x.size(); i-- > 0; ) ...
- delinka 12y agoNow i is no longer an index, it's index-plus-one.
- Aldo_MX 12y agoNope, it's still an index, after the comparison and before executing the code inside the loop, i is decreased by 1. Remember that a for loop does something like this behind the scenes: size_t i = x.size(); while( i-- > 0 ) { // your code which needs backwards iteration here ; // do nothing because there is no third statement }
- shdon 12y agoNo, it's not. The check is done before the loop, so i has the correct value inside the loop. Still, the trick makes it look suspect and that's an argument against using it.
- Aldo_MX 12y ago> Still, the trick makes it look suspect and that's an argument against using it. This is true. The code is confusing to people not used to it. A workaround could be to hide this code inside a macro, so people not interested in digging into the code would take the macro's word: #define REVERSE_LOOP( x, i ) for( size_t i = x.size(); i-- > 0; ) But unfortunately, that doesn't help with the fear that people has against unsigned types.
- stinos 12y agoI have to do these stuff: for (int i = x.size() - 1; i >= 0; i--) . That yields compiler warnings for signed vs unsigned then, no?
- detrino 12y ago
- TeMPOraL 12y ago> I wonder why we have developed this habit of defaulting all int as signed and consider uint as taboo (most coding guidelines asks not to use them unless "you know what you are doing"). I'm pretty sure that it's just because "int" is one word and "unsigned int" is two, plus more than twice the characters. I suspect if "int" defaulted to "unsigned int" and you'd have to specify signed ints explicitly, the taboo would be reversed. Never underestimate the power of trivial inconveniences.