Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++.moderated > #7281

Re: Avoid 'int' and associates.

Message-ID <mlki78$918$1@dont-email.me> (permalink)
Newsgroups comp.lang.c++.moderated, comp.lang.c++
From James Kuyper <jameskuyper@verizon.net>
Subject Re: Avoid 'int' and associates.
Organization A noiseless patient Spider
References <cZOdnSRj9NsZweHInZ2dnUU7-IudnZ2d@giganews.com>
Date 2015-06-14 18:11 -0600

Cross-posted to 2 groups.

Show all headers | View raw


On 06/14/2015 08:36 AM, Mr Flibble wrote:
> 
> In modern C++ we should avoid using 'int' and its associates such as 
> 'short' and 'long' as they are all non-portable and unsafe (their size 
> and value range can differ from one implementation to the next); instead 
> one should use the typedefs from <cstdint> instead.
> 
> A consequence of this rule is that we must never use 'auto' with integer 
> literals such as:
> 
> auto i = 42;
> 
> or
> 
> auto i = 42u;
> 
> instead we write:
> 
> int32_t i = 42;
> 
> or
> 
> uint32_t i = 42u;
> 
> The fact we have to do this is due to shortcoming of the current C++ 
> Standard; what we need is something like:
> 
> auto i = 42s32; // signed, 32-bits
> 
> or
> 
> auto i = 42u32; // unsigned, 32-bits

That's going a bit too far, in my opinion. First of all, whenever using
an interface, it's generally best to use the types specified for that
interface, even if they include 'int', 'short', or 'long'.

Secondly, if there's a traditional type name that's essentially
equivalent to one of the size-named types, I'll normally use the
traditional name instead, simply to save on my typing (though the
savings are bigger for the signed types). Types I consider essentially
equivalent are:

[u]int_least8_t     [un]signed char
[u]int_least16_t    [unsigned] short
[u]int_fast16_t     [unsigned] int
[u]int_fast32_t     [unsigned] long

Much of my code is targeted specifically at POSIX, which guarantees that
CHAR_BIT == 8, in which case I'm also willing to treat [un]signed char
as equivalent to [u]int8_t, but only if I've inserted the following
protection in the same translation unit:

#include <limits.h>
#if CHAR_BIT != 8
#error CHAR_BIT != 8
#endif
-- 
James Kuyper


      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

Back to comp.lang.c++.moderated | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Avoid 'int' and associates. Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk> - 2015-06-14 06:36 -0600
  Re: Avoid 'int' and associates. Melzzzzz <mel@zzzzz.com> - 2015-06-14 18:00 -0600
  Re: Avoid 'int' and associates. Francis Glassborow <francis.glassborow@btinternet.com> - 2015-06-14 18:07 -0600
  Re: Avoid 'int' and associates. "James K. Lowden" <jklowden@speakeasy.net> - 2015-06-14 18:11 -0600
  Re: Avoid 'int' and associates. James Kuyper <jameskuyper@verizon.net> - 2015-06-14 18:11 -0600
  Re: Avoid 'int' and associates. Maciej Sobczak <see.my.homepage@googlemail.com> - 2015-06-14 18:11 -0600
  Re: Avoid 'int' and associates. Paavo Helde <myfirstname@osa.pri.ee> - 2015-06-14 20:24 -0600
    Re: Avoid 'int' and associates. David Brown <david.brown@hesbynett.no> - 2015-06-15 07:13 -0600
      Re: Avoid 'int' and associates. Francis Glassborow <francis.glassborow@btinternet.com> - 2015-06-15 11:22 -0600
        Re: Avoid 'int' and associates. Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk> - 2015-06-15 13:02 -0600
          Re: Avoid 'int' and associates. Francis Glassborow <francis.glassborow@btinternet.com> - 2015-06-15 14:54 -0600
            Re: Avoid 'int' and associates. Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk> - 2015-06-15 15:54 -0600
              Re: Avoid 'int' and associates. James Kuyper <jameskuyper@verizon.net> - 2015-06-15 21:04 -0600
                Re: Avoid 'int' and associates. David Brown <david.brown@hesbynett.no> - 2015-06-16 07:05 -0600
                Re: Avoid 'int' and associates. James Kuyper <jameskuyper@verizon.net> - 2015-06-16 14:59 -0600
                Re: Avoid 'int' and associates. David Brown <david.brown@hesbynett.no> - 2015-06-16 16:47 -0600
              Re: Avoid 'int' and associates. scott@slp53.sl.home (Scott Lurndal) - 2015-06-16 09:54 -0600
                Re: Avoid 'int' and associates. legalize+jeeves@mail.xmission.com (Richard) - 2015-06-16 14:59 -0600
                Re: Avoid 'int' and associates. Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk> - 2015-06-16 16:47 -0600
              Re: Avoid 'int' and associates. "Chris M. Thomasson" <nospam@nospam.nospam> - 2015-06-16 15:03 -0600
                Re: Avoid 'int' and associates. Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk> - 2015-06-16 16:47 -0600
                Re: Avoid 'int' and associates. pip010 <pip010@googlemail.com> - 2015-08-03 06:54 -0600
                Re: Avoid 'int' and associates. maddoxr@acm.org - 2015-08-03 10:01 -0600
            Re: Avoid 'int' and associates. James Kuyper <jameskuyper@verizon.net> - 2015-06-15 21:04 -0600
          Re: Avoid 'int' and associates. James Kuyper <jameskuyper@verizon.net> - 2015-06-15 15:55 -0600
        Re: Avoid 'int' and associates. David Brown <david.brown@hesbynett.no> - 2015-06-16 07:04 -0600
  Re: Avoid 'int' and associates. Öö Tiib <ootiib@hot.ee> - 2015-06-27 14:17 -0600

csiph-web