473,472 Members | 2,038 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Platform independent types

The recent discussion on PIMPL and opaque pointers made me think about
a construct I have seen commonly with respect to defining types on a
platform by platform basis. Unlike structs, obviously types needs to
be 'complete' in header files for ease of use.

What I mean is something like the following...
#ifdef PLATFORM1
typedef unsigned long unsigned64;
....
#elseif PLATFORM2
typedef unsigned long long unsigned64;
#endif

That way you can ensure that a given type is the same size,
independent of platform.

With all the vitriol against "#if", etc, does anyone have any other
methods for achieving the same sort of effect?

Thanks
Jun 27 '08 #1
10 3671
joseph cook wrote:
The recent discussion on PIMPL and opaque pointers made me think about
a construct I have seen commonly with respect to defining types on a
platform by platform basis. Unlike structs, obviously types needs to
be 'complete' in header files for ease of use.

What I mean is something like the following...
#ifdef PLATFORM1
typedef unsigned long unsigned64;
...
#elseif PLATFORM2
typedef unsigned long long unsigned64;
#endif

That way you can ensure that a given type is the same size,
independent of platform.

With all the vitriol against "#if", etc, does anyone have any other
methods for achieving the same sort of effect?
See header <inttypes.h>

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 27 '08 #2
See header <inttypes.h>
But that's a C99 header, not a C++ Header, right?
Jun 27 '08 #3
joseph cook wrote:
>See header <inttypes.h>

But that's a C99 header, not a C++ Header, right?
Most compilers already provide it. And it's part of C++0x specification.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 28 '08 #4
On Jun 27, 4:57*pm, Victor Bazarov <v.Abaza...@comAcast.netwrote:
joseph cook wrote:
See header <inttypes.h>
But that's a C99 header, not a C++ Header, right?

Most compilers already provide it. *And it's part of C++0x specification.
Technically, the C++ version of this header file will be named
<cinttypes>. However, the <cinttypesheader is probably not the best
choice to look for platform-independent type names - because it
defines macros with names like "SCNiFAST64" and "SCNo32" - names which
some may find a little cryptic.

Fortunately, C++09x will also include the header <cstdintwhich -
instead of macros - declares various typedefs with names like
"int32_t" and "uint_fast64_t" which, as types names, are probably a
little clearer than the names #defined in <cinttypes>.

Greg
Jun 28 '08 #5
Fortunately, C++09x will also include the header <cstdintwhich -
instead of macros - declares various typedefs with names like
"int32_t" and "uint_fast64_t" which, as types names, are probably a
little clearer than the names #defined in <cinttypes>.

Greg
Thanks! It's good to see that getting added to the standard. I guess
this is, as I expected, a valid use of #if's in C++ circa 2008, which
will soon be eliminated.
Jun 28 '08 #6
Greg Herlihy schrieb:
On Jun 27, 4:57 pm, Victor Bazarov <v.Abaza...@comAcast.netwrote:
>joseph cook wrote:
>>>See header <inttypes.h>
But that's a C99 header, not a C++ Header, right?
Most compilers already provide it. And it's part of C++0x specification.

Technically, the C++ version of this header file will be named
<cinttypes>. However, the <cinttypesheader is probably not the best
choice to look for platform-independent type names - because it
defines macros with names like "SCNiFAST64" and "SCNo32" - names which
some may find a little cryptic.

Fortunately, C++09x will also include the header <cstdintwhich -
instead of macros - declares various typedefs with names like
"int32_t" and "uint_fast64_t" which, as types names, are probably a
little clearer than the names #defined in <cinttypes>.

Greg

cant include with #include <cstdint>, #include <stdint.hworks. is this
compiler-dependent?

cheers, chris
Jun 28 '08 #7
On Jun 27, 11:39 pm, joseph cook <joec...@gmail.comwrote:
The recent discussion on PIMPL and opaque pointers made me
think about a construct I have seen commonly with respect to
defining types on a platform by platform basis. Unlike
structs, obviously types needs to be 'complete' in header
files for ease of use.
What I mean is something like the following...
#ifdef PLATFORM1
typedef unsigned long unsigned64;
...
#elseif PLATFORM2
typedef unsigned long long unsigned64;
#endif
That way you can ensure that a given type is the same size,
independent of platform.
With all the vitriol against "#if", etc, does anyone have any
other methods for achieving the same sort of effect?
As Victor said, you include <stdint.h>. Or if portability is a
concern, <stdint.hh>, or <stdint.hpp>, or <mystdint.hhor
whatever. A header file in a platform dependent directory,
selected by the -I (or /I) option when you compile.

In this case, the implementation of this header for most systems
is to just include <stdint.h>. If you do stumble on an
implementation which doesn't support it, however, you provide
the necessary code by hand.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jun 28 '08 #8
On Jun 28, 4:19 am, joseph cook <joec...@gmail.comwrote:
Fortunately, C++09x will also include the header <cstdint>
which - instead of macros - declares various typedefs with
names like "int32_t" and "uint_fast64_t" which, as types
names, are probably a little clearer than the names #defined
in <cinttypes>.
Thanks! It's good to see that getting added to the standard.
I guess this is, as I expected, a valid use of #if's in C++
circa 2008, which will soon be eliminated.
No, it's not a valid use of #if's. I've used my own stdint.hh
for years now, with no #if's.

In any reasonable project, there's a directory per target
platform, for the platform specific stuff. You don't mix stuff
for different platforms in the same file; you choose which file
you include by means of a -I (or /I) option to the compiler.

There is one valid use of #if's: include guards, but that's the
only one I've ever really found.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jun 28 '08 #9
On 2008-06-28 09:02, Chris Forone wrote:
Greg Herlihy schrieb:
>On Jun 27, 4:57 pm, Victor Bazarov <v.Abaza...@comAcast.netwrote:
>>joseph cook wrote:
See header <inttypes.h>
But that's a C99 header, not a C++ Header, right?
Most compilers already provide it. And it's part of C++0x specification.

Technically, the C++ version of this header file will be named
<cinttypes>. However, the <cinttypesheader is probably not the best
choice to look for platform-independent type names - because it
defines macros with names like "SCNiFAST64" and "SCNo32" - names which
some may find a little cryptic.

Fortunately, C++09x will also include the header <cstdintwhich -
instead of macros - declares various typedefs with names like
"int32_t" and "uint_fast64_t" which, as types names, are probably a
little clearer than the names #defined in <cinttypes>.

Greg


cant include with #include <cstdint>, #include <stdint.hworks. is this
compiler-dependent?
As others have pointed out, currently there is no stdint in C++ (but it
will be in C++09, where it will be called <cstdint>), but you can use
the C version of the file <stdint.h>.

--
Erik Wikström
Jun 28 '08 #10
Erik Wikström schrieb:
On 2008-06-28 09:02, Chris Forone wrote:
>Greg Herlihy schrieb:
>>On Jun 27, 4:57 pm, Victor Bazarov <v.Abaza...@comAcast.netwrote:
joseph cook wrote:
>See header <inttypes.h>
But that's a C99 header, not a C++ Header, right?
Most compilers already provide it. And it's part of C++0x specification.
Technically, the C++ version of this header file will be named
<cinttypes>. However, the <cinttypesheader is probably not the best
choice to look for platform-independent type names - because it
defines macros with names like "SCNiFAST64" and "SCNo32" - names which
some may find a little cryptic.

Fortunately, C++09x will also include the header <cstdintwhich -
instead of macros - declares various typedefs with names like
"int32_t" and "uint_fast64_t" which, as types names, are probably a
little clearer than the names #defined in <cinttypes>.

Greg

cant include with #include <cstdint>, #include <stdint.hworks. is this
compiler-dependent?

As others have pointed out, currently there is no stdint in C++ (but it
will be in C++09, where it will be called <cstdint>), but you can use
the C version of the file <stdint.h>.
thanks! cheers, chris
Jun 29 '08 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: RA Scheltema | last post by:
hi all, A small question about serializing and deserializing a long in a platform independent manner. Can this be done with the following code ?: char buf; long val = 35456;
14
by: John Salerno | last post by:
Bear with me, but I've been reading a lot about how the .NET languages are platform independent, and I assume this means a program written in C# can be run on a Unix or Mac machine. If this...
16
by: Andy | last post by:
Hi, I have read that 'C' is platform-independent and portable. I can'tsee much a difference between the two terms. Could anyone differentiate the two? Also is the statement actually true? Thanks...
16
by: bobrik | last post by:
Hello, I am using the Python DB API for access to MySQL. But it is not platform-independent - I need a module not included in Python by default - python-mysql, and it uses a compiled binary...
22
by: David T. Ashley | last post by:
I'm writing code which will compile on multiple Windows and multiple *nix platforms. Some will have 64-bit integers, I believe. What is my best option to define platform-independent types such...
8
by: =?iso-8859-1?B?QW5kcuk=?= | last post by:
I would like to find out how I can launch an independent Python program from existing one in a cross-platform way. The result I am after is that a new terminal window should open (for io...
3
by: saneman | last post by:
I have read that Python is a platform independent language. But on this page: http://docs.python.org/tut/node4.html#SECTION004220000000000000000 it seems that making a python script...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.