PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.cplus > Platform independent types
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Platform independent types

Réponse
 
LinkBack Outils de la discussion
Vieux 27/06/2008, 22h39   #1
joseph cook
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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
  Réponse avec citation
Vieux 27/06/2008, 22h52   #2
Victor Bazarov
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Platform independent types

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
  Réponse avec citation
Vieux 27/06/2008, 23h09   #3
joseph cook
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Platform independent types


> See header <inttypes.h>
>


But that's a C99 header, not a C++ Header, right?
  Réponse avec citation
Vieux 28/06/2008, 00h57   #4
Victor Bazarov
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Platform independent types

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
  Réponse avec citation
Vieux 28/06/2008, 03h05   #5
Greg Herlihy
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Platform independent types

On Jun 27, 4:57pm, Victor Bazarov <v.Abaza...@comAcast.net> 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


  Réponse avec citation
Vieux 28/06/2008, 03h19   #6
joseph cook
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Platform independent types


> 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.
  Réponse avec citation
Vieux 28/06/2008, 08h02   #7
Chris Forone
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Platform independent types

Greg Herlihy schrieb:
> On Jun 27, 4:57 pm, Victor Bazarov <v.Abaza...@comAcast.net> 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
  Réponse avec citation
Vieux 28/06/2008, 09h00   #8
James Kanze
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Platform independent types

On Jun 27, 11:39 pm, joseph cook <joec...@gmail.com> 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:james.kanze@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
  Réponse avec citation
Vieux 28/06/2008, 09h03   #9
James Kanze
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Platform independent types

On Jun 28, 4:19 am, joseph cook <joec...@gmail.com> 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:james.kanze@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
  Réponse avec citation
Vieux 28/06/2008, 11h18   #10
Erik Wikström
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Platform independent types

On 2008-06-28 09:02, Chris Forone wrote:
> Greg Herlihy schrieb:
>> On Jun 27, 4:57 pm, Victor Bazarov <v.Abaza...@comAcast.net> 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
  Réponse avec citation
Vieux 29/06/2008, 07h15   #11
Chris Forone
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Platform independent types

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.net> 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
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 03h07.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,18942 seconds with 19 queries