4 ms·
Can anyone shed some light on the design rationale for using integer types (most commonly int it seems, as here) followed by a check if the number is not negati
by stinos 11y ago
Can anyone shed some light on the design rationale for using integer types (most commonly int it seems, as here) followed by a check if the number is not negative, whereas one could just use an unsigned type right away?
- hthh 11y agoI disagree with it, but the Google C++ Style Guide offers the rationale you're looking for (under "On Unsigned Integers"): https://google-styleguide.googlecode.com/svn/trunk/cppguide.html#Integer_Types https://google-styleguide.googlecode.com/svn/trunk/cppguide....
- raverbashing 11y agoYeah this doesn't convince me The bug example there, sure, needs a signed type, so you can't blame the type for its wrong usage I've had more bugs coming from using signed types that I'll not be bothered by writing 'unsigned' ever again
- detrino 11y agoI disagree that a signed type is the correct solution to that, rather you need a while loop: std::size_t i = foo.size(); while (i != 0) { --i; ... }
- gohrt 11y agoThe point is that C int types are an unsafe mess, so it's better to have one simple rule than memorize all the corner cases and address them all the time.
- deleted 11y ago[deleted]
- detrino 11y agoI am addressing the specific example given by the Google style guide. The code for counting down using a signed integer as they do has more corner cases than the while loop I have shown. Using their way, you have to remember to subtract 1 from the size at the start and then use >= in the loop conditional. My way is just the inverse of what you do while counting up. It's also worth pointing out the style of loop they give can't be used at all if you are counting down iterators or pointers instead of numbers.
- stinos 11y agoHmm, that seems a rather weak argument indeed? Purely anecdotical, but the number of bugs I'v seen which stem from using int and then failing to check if it is negative (followed by using it as index or converting to unsigned) by far outweigh the number of times I even saw the type of loop they mention as a con.
- zurn 11y agoDifferent semantics: int overflow is undefined but can handle negative values, unsigned wraps around. So you would use int if your expression can yield a negative value. (You can't thus check for int overflow by checking for wraparound, see http://c-faq.com/misc/intovf.html http://c-faq.com/misc/intovf.html)
- stinos 11y ago> So you would use int if your expression can yield a negative value. Of course, but as you say it yourself: if. It seems a bit too general to abandon unsigned completely because there are cases where it is not appropriate. By that logic there wouldn't be much types one can use at all.
- Someone 11y agoFor return values of a function it allows overloading, with negative numbers indicating errors. That's useful if your language lacks the ability to return multiple values from a function. That typically was (probably still is on quite a few architectures) the case for languages designed for speed. Also, in C, int was implicit (see https://github.com/mortdeus/legacy-cc https://github.com/mortdeus/legacy-cc for example source code). So, using int made your programs shorter. That's important if your multi-user system doesn't have much memory (the first PDP-11 that ran Unix had 24 kilobytes of memory), and if you like concise porgrams, as Ritchie apparently did.
- khuttun 11y agoScott Meyers offers his rationale here: http://www.aristeia.com/Papers/C++ReportColumns/sep95.pdf http://www.aristeia.com/Papers/C++ReportColumns/sep95.pdf
- zvrba 11y agoSee my answer here: https://news.ycombinator.com/item?id=9850833 https://news.ycombinator.com/item?id=9850833