Platform independent types

Discussion in 'C++' started by joseph cook, Jun 27, 2008.

  1. joseph  cook

    joseph cook Guest

    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
    joseph cook, Jun 27, 2008
    #1
    1. Advertising

  2. joseph  cook

    joseph cook Guest


    > See header <inttypes.h>
    >


    But that's a C99 header, not a C++ Header, right?
    joseph cook, Jun 27, 2008
    #2
    1. Advertising

  3. joseph  cook

    Greg Herlihy Guest

    On Jun 27, 4:57 pm, Victor Bazarov <> wrote:
    > 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 <cinttypes> header 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 <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>.

    Greg
    Greg Herlihy, Jun 28, 2008
    #3
  4. joseph  cook

    joseph cook Guest


    > 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>.
    >
    > 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.
    joseph cook, Jun 28, 2008
    #4
  5. joseph  cook

    Chris Forone Guest

    Greg Herlihy schrieb:
    > On Jun 27, 4:57 pm, Victor Bazarov <> wrote:
    >> 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 <cinttypes> header 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 <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>.
    >
    > Greg
    >
    >


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

    cheers, chris
    Chris Forone, Jun 28, 2008
    #5
  6. joseph  cook

    James Kanze Guest

    On Jun 27, 11:39 pm, 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?


    As Victor said, you include <stdint.h>. Or if portability is a
    concern, <stdint.hh>, or <stdint.hpp>, or <mystdint.hh> or
    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:
    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
    James Kanze, Jun 28, 2008
    #6
  7. joseph  cook

    James Kanze Guest

    On Jun 28, 4:19 am, joseph cook <> wrote:
    > > 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:
    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
    James Kanze, Jun 28, 2008
    #7
  8. On 2008-06-28 09:02, Chris Forone wrote:
    > Greg Herlihy schrieb:
    >> On Jun 27, 4:57 pm, Victor Bazarov <> wrote:
    >>> 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 <cinttypes> header 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 <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>.
    >>
    >> Greg
    >>
    >>

    >
    > cant include with #include <cstdint>, #include <stdint.h> works. 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
    Erik Wikström, Jun 28, 2008
    #8
  9. joseph  cook

    Chris Forone Guest

    Erik Wikström schrieb:
    > On 2008-06-28 09:02, Chris Forone wrote:
    >> Greg Herlihy schrieb:
    >>> On Jun 27, 4:57 pm, Victor Bazarov <> wrote:
    >>>> 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 <cinttypes> header 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 <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>.
    >>>
    >>> Greg
    >>>
    >>>

    >> cant include with #include <cstdint>, #include <stdint.h> works. 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
    Chris Forone, Jun 29, 2008
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ibeetb
    Replies:
    1
    Views:
    441
    Steve C. Orr [MVP, MCSD]
    Jun 1, 2004
  2. Harald Hein
    Replies:
    9
    Views:
    422
    Andrew Thompson
    Aug 17, 2003
  3. RA Scheltema
    Replies:
    10
    Views:
    551
    Joona I Palaste
    Jan 24, 2004
  4. RA Scheltema

    platform independent serialization of a long

    RA Scheltema, Jan 23, 2004, in forum: C Programming
    Replies:
    10
    Views:
    592
    Joona I Palaste
    Jan 24, 2004
  5. MK
    Replies:
    1
    Views:
    837
    Peter Hansen
    Jun 25, 2003
Loading...

Share This Page