Connecting Tech Pros Worldwide Help | Site Map

Testing for a type

bytebro
Guest
 
Posts: n/a
#1: Mar 23 '07

Is there any way of testing at compile time (i.e. by the preprocessor)
whether a particular type has been declared?

I guess what I'm looking for is something like

#if defined(uint8_t)
....
#endif

although of course I know that particular construction doesn't work.
I've also tried

#if sizeof(uint8_t) == 1

but that doesn't work either

Thanks!

Eric Sosman
Guest
 
Posts: n/a
#2: Mar 23 '07

re: Testing for a type


bytebro wrote:
Quote:
Is there any way of testing at compile time (i.e. by the preprocessor)
whether a particular type has been declared?
Not directly, no, because types don't come into existence
until after the preprocessor has finished. Keywords like `int'
and `typedef' are merely source tokens to the preprocessor; they
aren't even recognized as keywords until later in compilation.
Quote:
I guess what I'm looking for is something like
>
#if defined(uint8_t)
...
#endif
>
although of course I know that particular construction doesn't work.
What is usually done is to #define a preprocessor macro in
the same header that declares the type(s) of interest:

typedef unsigned char uint8_t;
#define UINT8_T_DEFINED

.... and then you can test for the macro

#ifdef UINT8_T_DEFINED

Note that `uint8_t' is one of the types from <stdint.hin
C99-conforming implementations. If you are using <stdint.h(as
opposed to writing pre-C99 "free-hand" declarations of your own),
then the macro `UINT8_MAX' is defined if `uint8_t' exists.
Quote:
I've also tried
>
#if sizeof(uint8_t) == 1
>
but that doesn't work either
Since types themselves don't exist during preprocessing, the
sizes of types also don't exist. (In fact, `sizeof' is not even
recognized as an operator this early in the game.)

--
Eric Sosman
esosman@acm-dot-org.invalid
bytebro
Guest
 
Posts: n/a
#3: Mar 23 '07

re: Testing for a type


On 23 Mar, 14:13, Eric Sosman <esos...@acm-dot-org.invalidwrote:
Quote:
>
What is usually done is to #define a preprocessor macro in
the same header that declares the type(s) of interest:
>
typedef unsigned char uint8_t;
#define UINT8_T_DEFINED
>
... and then you can test for the macro
>
#ifdef UINT8_T_DEFINED
>
Note that `uint8_t' is one of the types from <stdint.hin
C99-conforming implementations. If you are using <stdint.h(as
I'm using gcc 2.95.3 on SunOS 5.6 :( The types are defined in
<inttypes.h(actually in <sys/int_types.h>), and sadly the per-type
defines you describe are not done.

Never mind; I'll put together some kind of configure script that sets
the necessary defines.

And thanks for the advice.

Closed Thread