I want to have conditional macros inside a macro. When I compile this
code, the error message is:
ONetCDFFile.cpp :14:2: '#' is not followed by a macro parameter
Any idea how I should proceed with this?
Re: is it possible to write such a macro?
another question about macro, how do i force a return at the end of
each macro line?
it seems the end result of multi-line macro always colapse into a
single line...
[email protected] om
wrote:[color=blue]
> #define ONCFILE_ERR1(fu ncname, name) \
> #ifdef DEBUG\
> cerr << __FILE__ << ":" << __LINE__ << " duplicated call to " <<
> funcname << " " << name << endl; \
> #endif \
> I want to have conditional macros inside a macro. When I compile this
> code, the error message is:
> ONetCDFFile.cpp :14:2: '#' is not followed by a macro parameter
> Any idea how I should proceed with this?[/color]
> I want to have conditional macros inside a macro. When I compile this
> code, the error message is:
> ONetCDFFile.cpp :14:2: '#' is not followed by a macro parameter
> Any idea how I should proceed with this?[/color]
Your code is C++, so asking in comp.lang.c++ is
better than here. However I don't think the preprocessor
is different. Think about how \ works with lines being
spliced together. Your #ifdef is in the
middle of a line... However you
could achieve what you want above with
#ifdef DEBUG
#define ONCFILE_ERR1(fu ncname, name) \
{ some code; }
#else
#define ONCFILE_ERR1(fu ncname,name)
#endif
-David
> I want to have conditional macros inside a macro. When I compile this
> code, the error message is:
> ONetCDFFile.cpp :14:2: '#' is not followed by a macro parameter
> Any idea how I should proceed with this?
>[/color]
Turn it inside out...
#ifdef DEBUG
#define ONCFILE_ERR1(fu ncname, name) \
cerr << __FILE__ << ":" << __LINE__ << " duplicated call to " \
<< funcname << " " << name << endl
#else
#define ONCFILE_ERR1(fu ncname, name)
#endif
Artie Gold -- Austin, Texas
I will (ir)regularly write about things that are important to me -- that I hope interest you.
Of course, since I see most things as being political, that will be most of it.
"You can't KISS* unless you MISS**"
[*-Keep it simple, stupid. **-Make it simple, stupid.]
Re: is it possible to write such a macro?
Thank you for your reply..sorry about the c++ code although my point is
really just the preprocessing macro. Your suggestions work for this
simple macro, what if I have something more complicated such as
#define macro1
do stuff
#ifdef c1
do something
#endif
do stuff
#ifdef c2
do something else
#endif
.....
#endif /* macro1 */
as you can see, if it's possible to embed "#ifdef" etc inside #define,
things are much easier. Is this possible?
David Resnick wrote:[color=blue]
>
[email protected] om
wrote:[color=green]
> > #define ONCFILE_ERR1(fu ncname, name) \
> > #ifdef DEBUG\
> > cerr << __FILE__ << ":" << __LINE__ << " duplicated call to " <<
> > funcname << " " << name << endl; \
> > #endif \
> > I want to have conditional macros inside a macro. When I compile this
> > code, the error message is:
> > ONetCDFFile.cpp :14:2: '#' is not followed by a macro parameter
> > Any idea how I should proceed with this?[/color]
> Your code is C++, so asking in comp.lang.c++ is
> better than here. However I don't think the preprocessor
> is different. Think about how \ works with lines being
> spliced together. Your #ifdef is in the
> middle of a line... However you
> could achieve what you want above with
> #ifdef DEBUG
> #define ONCFILE_ERR1(fu ncname, name) \
> { some code; }
> #else
> #define ONCFILE_ERR1(fu ncname,name)
> #endif
> -David[/color]
[email protected] om
wrote:[color=blue]
> Thank you for your reply..sorry about the c++ code although my point is
> really just the preprocessing macro. Your suggestions work for this
> simple macro, what if I have something more complicated such as
> #define macro1
> do stuff
> #ifdef c1
> do something
> #endif
> do stuff
> #ifdef c2
> do something else
> #endif
> #endif /* macro1 */
> as you can see, if it's possible to embed "#ifdef" etc inside #define,
> things are much easier. Is this possible?
>[/color]
Artie Gold -- Austin, Texas
I will (ir)regularly write about things that are important to me -- that I hope interest you.
Of course, since I see most things as being political, that will be most of it.
"You can't KISS* unless you MISS**"
[*-Keep it simple, stupid. **-Make it simple, stupid.]
> I want to have conditional macros inside a macro. When I compile this
> code, the error message is:
> ONetCDFFile.cpp :14:2: '#' is not followed by a macro parameter
> Any idea how I should proceed with this?[/color]
The workaround already has been stated; back to the issue:
- Preprocessing directives can only span one line (the \ may hide
that but the above is only one source line) and there may be only
one preprocessing directive per line.
- Preprocessing directives start the respective line (but for
whitespace)
- Thus, you cannot generate preprocessing directives using
preprocessing directives; the language does not allow it.
The reason for the error message is another one:
The preprocessor assumes that the # is the "stringize" preprocessor
operator; this operator surrounds the passed argument by double
quotes ("), i.e. makes them into string literals.
Cheers
Michael
E-Mail: Mine is an /at/ gmx /dot/ de address.
>> I want to have conditional macros inside a macro. When I compile
>> this code, the error message is:
>> ONetCDFFile.cpp :14:2: '#' is not followed by a macro parameter
>> Any idea how I should proceed with this?[/color]
> another question about macro, how do i force a return at the end
> of each macro line? it seems the end result of multi-line macro
> always colapse into a single line...[/color]
Don't toppost. Your answer (or continuation) belongs after (or
intermixed with) the material you quote, after snipping irrelevant
material. I fixed this one.
You can't do that.
Ignoring the C++, and just considering macros (which are common to
C++ and C). By definition a macro (the part after the #define)
ends on the same line. This can be partially ameliorated by the
use of continuation lines (where the last character on the physical
line is a '\'). You CANNOT put preprocessing conditionals inside a
macro, because they have to start a line, and you can't get there
without ending the macro definition.
The usual trick to form multiline macros is the following:
#define MULTILINEMACRO \
do { \
statement1; \
statement2; \
} while (0)
Notice no final semicolon. The limit is the maximum input line the
compiler can handle, which is guaranteed about 500 for C90. Look
it up. This behaves properly for all macros that do not have to
return a value.
In future please ask C++ questions on c.l.c++, and C questions on
c.l.c. Don't toppost on either newsgroup.
Read about the Sony stealthware that is a security leak, phones
home, and is generally illegal in most parts of the world. Also
the apparent connivance of the various security software firms.