[lug] OT: CPP question
Tkil
tkil at scrye.com
Fri Mar 29 15:46:12 MST 2002
>>>>> "George" == George Sexton <Sexton> writes:
George> I have a question about CPP, and I was hoping someone could help me.
George> Given:
George> #ifdef THIS
George> #define MY_FUNC(x) SomeFunction(x)
George> #else
George> #define MY_FUNC(x) SomeOtherFunction(x)
George> #endif
George> MY_FUNC(some_literal);
George> If THIS is defined, I would like the output to be:
George> SomeFunction('some_literal');
George> but if THIS is not defined, I would like the output to be:
George> SomeOtherFunction(some_literal);
do you really want single quotes? that's harder.
if you want double-quotes, use the "stringify" preprocessor operator,
a single hash/pound/octothorpe character, something like this:
#ifdef DEBUG
# define assert(x) \
do { \
int v = (x); \
if (!v) { \
fprintf(stderr, "%s:%s: assertion \"%s\" failed.\n", \
__FILE__, __LINE__, #x); \
} \
while (0)
#else /* DEBUG */
# define assert(x)
#endif /* DEBUG */
or similar.
George> I tried MY_FUNC(x) SomeFunction('x') but it gives:
George> SomeFunction('x');
George> I tried MY_FUNC(x) SomeFunction(''x'') but it gives:
George> SomeFunction(''some_literal'');
ansi cpp uses token-based macro expansion, so working with strings is
occasionally painful. they introduced "#" (stringify) and "##" (paste
tokens) to help with this.
or did i totally misunderstand your question?
George> Does anyone have an idea on how I can create the macro so that
George> in one form the output is the literal single quoted, but in
George> another it is just the literal?
single-quoting will be hard, i fear. hm... double-expansion might do
it. hm, not that i can find -- getting a preprocessing token defined
to be a single quote would help, i think, but i can't seem to do it
obviously. the "cpp(Stringification)" info page flatly states:
There is no way to convert a macro argument into a character
constant.
(well, at least the a version i found on the web does -- the one on
scrye doesn't seem to have that in it. peculiar. probably version
differences.)
what are you doing that you want single-quoted output anyway? is cpp
the right tool, or should you investigate m4?
t.
More information about the LUG
mailing list