Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++.moderated > #7286
| Message-ID | <bcKdnd8ZC4KYmOLInZ2dnUU7-VednZ2d@giganews.com> (permalink) |
|---|---|
| Newsgroups | comp.lang.c++.moderated, comp.lang.c++ |
| From | Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk> |
| Subject | Re: Avoid 'int' and associates. |
| Organization | unknown |
| References | <cZOdnSRj9NsZweHInZ2dnUU7-IudnZ2d@giganews.com> <XnsA4BA1E81F95F9myfirstnameosapriee@216.166.105.131> <mllsre$3ni$1@dont-email.me> <mlmm4b$ueu$1@dont-email.me> |
| Date | 2015-06-15 13:02 -0600 |
Cross-posted to 2 groups.
On 15/06/2015 18:22, Francis Glassborow wrote:
>
> On 15/06/2015 14:13, David Brown wrote:
>>
>> On 15/06/15 04:24, Paavo Helde wrote:
>>> Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk> wrote in
>>> news:cZOdnSRj9NsZweHInZ2dnUU7-IudnZ2d@giganews.com:
>>>
>>>>
>>>> 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.
>>>
>>> Sorry, int is extremely portable, every C and C++ implementation has it
>>> and it is guaranteed to work in a pretty large range (the actual range
>>> can be checked quite easily via std::numeric_limits, BTW).
>>>
>>
>> This all comes down to what is meant by "portable". An integer type has
>> several characteristics, such as availability, minimum range, size, etc.
>> For some of these, "int" is highly portable - it is found on any C++
>> system, and is guaranteed a minimum range of -32767 to +32767. But
>> other characteristics, such as its actual size, lack portability.
>> "int32_t" (and friends) on the other hand do not exist on all systems -
>> but when they /do/ exist, they give you cross-target portability
>> guarantees on size and range that you cannot get with plain "int".
>>
>> If you need some or all of the additional characteristics of int32_t,
>> then int32_t is the most portable way to get that. If you just want a
>> counter or index, with a range up to +/- 32767, then "int" is the most
>> portable choice.
>>
>>
>> We should use the most appropriate type for the task in hand (which
>> could well be "auto") - sometimes it is "int", sometimes it is "int32_t".
>>
>> Personally, I have very little use of "short" or "long", however.
>
> So both C and C++ provide an integer type that is guaranteed to be at
> least 32-bits, so why would I not use that when I need a 32-bit integer?
> That is why int32_t better? Note that on systems with 32-bit int, long
> int is usually also 32 bits.
'int' is only guaranteed to be at least 16 bits but the actual problem
is the *at least* part: it can mean *different* sizes and value ranges
on *different* implementations resulting in *different* or even
*dangerous behaviour*. If you want a type that behaves like 'int' then
you should be explicit about it in your code by using 'int_fast16_t' but
absolute care must be taken when using such variable types.
>
> And where I need 64 bits the (ugly) long long int meets my need.
>
> If I use real types I do not have to worry about places where the
> behaviour varies according to the underlying type (most commonly i/o in C)
You should be writing code such that the underlying type doesn't
actually matter; that is part of the rationale for <cstdint> in the
first place.
/Flibble
--
[ 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
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