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


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

Re: Avoid 'int' and associates.

Message-ID <GOydnYro6PiNEB3InZ2dnUU7-e-dnZ2d@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> <mlna37$md5$1@dont-email.me> <y6OdnUG8htn2rOLInZ2dnUU7-KOdnZ2d@giganews.com> <utVfx.149824$Lj1.95451@fx07.iad> <mlpm56$sg8$1@news.xmission.com>
Date 2015-06-16 16:47 -0600

Cross-posted to 2 groups.

Show all headers | View raw


On 16/06/2015 21:59, Richard wrote:
> [Please do not mail me a copy of your followup]
>
> slp53@pacbell.net spake the secret code
> <utVfx.149824$Lj1.95451@fx07.iad> thusly:
>
>> Granted most of that code ran sans OS (it was the OS), and required
>> fixed types to map to various hardware registers.
>
> This is the situation where I think the size of the data really *does*
> matter and it's important to use sized types and not the implicit size
> of int, long, etc.  Things like structures representing byte streams
> passed across the network (and you might do network-to-host byte
> reordering in place on that structure), raw byte streams read from or
> written to files, raw bytes transmitted between processes through
> shared memory segments and so-on.
>
> I've got some code im my open source project that doesn't use
> specifically sized types for some binary file I/O and it's a mess right
> because they used generic types.  So I am certainly sympathetic to
> cases where it matters.
>
> My assertion is that it simply doesn't matter in *every* case.
>
>> However, absent API requirements for other types, I would prefer
>> using types for which I understand the characteristics in all
>> conditions, thus I prefer the explicitly sized types.  Having
>> run into many issues in the past porting software from 16-bit
>> ints to 32-bit ints (and from 32-bit longs to 64-bit longs),
>> I would never advocating using 'int' for anything.
>
> Here, I disagree.  The size of every int in a program isn't a
> portability concern.  What's important is deciding which variables need
> specific sizes and which don't.[*]
>
> I've seen code where the compiler's default size of an int was 16-bits
> and everywhere they wanted to iterate over containers or whatnot it was
> int16_t all over the place.  Then you move to a compiler where the
> defaault size of an int is 32-bits.  The fact that all those ints were
> marked as 16-bits is now erroneous and simply a distraction.  How do
> you know which ones really needed to be 16-bits and which were 16-bits
> simply because that was the default size of an int?  Forcing them all
> into a 16-bit straight jacket impedes portability instead of enhancing
> it.

This is why you should design things properly perhaps by the use of a 
traits class specialized for different hardware which has its own public 
typedefs:

template <typename HardwareType>
struct foo_traits;

template <>
struct foo_traits<ZXSpectrum48K>
{
	typedef uint16_t index_type;
};

typedef foo_traits<ZXSpectrum48K>::index_type index_type;

/* use index_type rather than uint16_t directly. */

/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 | NextPrevious in thread | Next in thread | Find similar


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