By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,655 Members | 964 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,655 IT Pros & Developers. It's quick & easy.

Testing for a type

P: n/a

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!

Mar 23 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
bytebro wrote:
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.
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.
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
es*****@acm-dot-org.invalid
Mar 23 '07 #2

P: n/a
On 23 Mar, 14:13, Eric Sosman <esos...@acm-dot-org.invalidwrote:
>
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.

Mar 23 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.