Which preprecessor directive will detect if a typedef exists yet? Eg #if IS_TYPE(sometype_t)?

J

James Kuyper

Kevin Smith wrote:
[Re: Which preprecessor directive will detect if a typedef exists yet?
Eg #if IS_TYPE(sometype_t)?]

Please don't rely upon the subject line to convey your question. It's
quite common for people to read the body of a message without even
looking at the subject line. Put the question itself in your post,
though it's OK to put a summary of the question in the subject line.

To answer your question, C provides no mechanism whereby your code can
test for the presence of a typedef. Even if C did provide one, it could
not be available for use in a #if: preprocessor directives are handled
during translation phase 4; typedefs don't get interpreted until
translation phase 7. If any method for doing this were added to C, it
would have to rely upon a phase 7 construct, perhaps
if(is_typedef(sometype_t)).

However, the main thing you would want to do with something like
is_typedef() is to control how the specified identifier is used in your
code. Unfortunately, both branches of the if statement would have to use
that identifier in a fashion that is legal, whether or not it is a
typedef, which defeats the only purpose it could reasonably serve.

It might be possible to get around this problem by splitting translation
phase 7 into 2 or more phases, but I've given the idea just enough
thought to realize that it would be horrendously difficult to do so.
 
H

Hallvard B Furuseth

Mark said:
As for detecting typedefs, I don't think you can though ICBW. The
preprocessor operates in an earlier stage of translation than the point
where typedefes get created.

The general ways to do it:

If it's your own type, you can wrap the definition in something like
#ifndef FOOBAR_TYPE_DEFINED
#define FOOBAR_TYPE_DEFINED
typedef ... Foobar;
#endif
or extend that to wrap the entire include-file which defines the
type, in #ifndef FILE_H_ / #define FILE_H_ / #endif

Otherwise #ifdef on something related to the type. E.g. only use
pthread_rwlock_t if PTHREAD_RWLOCK_INITIALIZER is #defined. Until
you port to a machine which defines one but not the other. Then

you switch to #defining HAVE_PTHREAD_RWLOCK_T in Makefile or a
yourapp-config.h file, maybe written by tools like autoconf or
metaconfig.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,444
Messages
2,571,709
Members
48,796
Latest member
Greg L.
Top