News for hackers. Stuff that matters.
Re:When ‘int’ is the new ‘short’ by stinos on Wednesday July 08, 2015 @03:21AM (#9850252)
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?
Re:When ‘int’ is the new ‘short’ by hthh on Wednesday July 08, 2015 @03:25AM (#9850262)
I 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....
Re:When ‘int’ is the new ‘short’ by stinos on Wednesday July 08, 2015 @04:55AM (#9850532)
Hmm, 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.
Re:When ‘int’ is the new ‘short’ by raverbashing on Wednesday July 08, 2015 @03:39AM (#9850302)
Yeah 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

Re:When ‘int’ is the new ‘short’ by detrino on Wednesday July 08, 2015 @04:12AM (#9850395)
I 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;
    ...
  }
Re:When ‘int’ is the new ‘short’ by gohrt on Wednesday July 08, 2015 @04:38AM (#9850476)
The 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.
Re:When ‘int’ is the new ‘short’ by detrino on Wednesday July 08, 2015 @05:20AM (#9850618)
I 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.

Re:When ‘int’ is the new ‘short’ by zurn on Wednesday July 08, 2015 @05:06AM (#9850572)
Different 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)

Re:When ‘int’ is the new ‘short’ by stinos on Wednesday July 08, 2015 @05:25AM (#9850636)
> 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.

Re:When ‘int’ is the new ‘short’ by khuttun on Wednesday July 08, 2015 @06:48AM (#9850831)
Scott Meyers offers his rationale here: http://www.aristeia.com/Papers/C++ReportColumns/sep95.pdf
Re:When ‘int’ is the new ‘short’ by Someone on Wednesday July 08, 2015 @06:42AM (#9850814)
For 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 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.

Re:When ‘int’ is the new ‘short’ by zvrba on Wednesday July 08, 2015 @06:50AM (#9850834)