Need your suggestion: a macro to define a type

S

sean

Hello, Everyone:
Recently I have a request to trrasfer some types between different
projects, i.e. one project is WORD, the other is unsigned short. So I
defnied a macro (TRANS2) to do this. However, the compiler just keep
"erroring" about this macro. Any one has idea about that ?

#define TRANS(x,y)
#ifdef x
#define x y
#endif

Usage:
#define TRANS(WORD,unsigned short) <= compile error..

Appreciate for any suggestions,
BR, MACRO newbie
 
S

Seebs

Hello, Everyone:
Recently I have a request to trrasfer some types between different
projects, i.e. one project is WORD, the other is unsigned short. So I
defnied a macro (TRANS2) to do this. However, the compiler just keep
"erroring" about this macro. Any one has idea about that ?

You can't make a macro that defines other macros. Macros are always one
line. They can have \ followed by additional "lines", but the additional
lines are all merged together.

So if you tried to do

#define TRANS(x,y) \
#ifdef x \
#define x y \
#endif

you'd end up with the equivalent of

#define TRANS(x,y) #ifdef x #define x y #endif
Usage:
#define TRANS(WORD,unsigned short) <= compile error..

So that would expand to

#ifdef WORD #define WORD unsigned short #endif

but because expansions never yield directives, that't just make it on in
to the compiler as a syntax error.

The best solution is probably completely eradicate the "WORD" type because
it isn't semantically coherent. Figure out what people were trying to do
and declare it correctly. If you have a C99 compiler, you can use stuff
like "int_least16_t", or you can just use short and int and long as
appropriate.

If you need something fast, just figure out what the specific types are, and
have an extra header you define that's full of junk like

#define WORD unsigned short

or more likely

typedef unsigned short WORD;

-s
 
S

sean

Dear Peter:

Thanks a lot for your kindly help. If so, then there is no way to
nest directives in directives?
Maybe I was wrong, but I remebered there are kinds of MACRO
expansion in MS's MFC to do so.

Br, MACRO newbie

Seebs 寫é“:
 
S

Seebs

Thanks a lot for your kindly help. If so, then there is no way to
nest directives in directives?

That's right.
Maybe I was wrong, but I remebered there are kinds of MACRO
expansion in MS's MFC to do so.

I seem to recall that MFC is C++, so it could be using templates, which can
do all sorts of crazy stuff. Or they might have some local extension.

But in plain old C, you can't have preprocessor directives create
other preprocessor directives.

-s
 
S

sean

Seebs 寫é“:
I seem to recall that MFC is C++, so it could be using templates, which can
do all sorts of crazy stuff. Or they might have some local extension.

But in plain old C, you can't have preprocessor directives create
other preprocessor directives.
Thanks alot, maybe I shall re-study the crazy Templates
sometims. :)
 

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

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top