is it possible to write such a macro?

F

fei.liu

#define ONCFILE_ERR1(funcname, 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?
 
F

fei.liu

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...
 
D

David Resnick

#define ONCFILE_ERR1(funcname, 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?

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(funcname, name) \
{ some code; }
#else
#define ONCFILE_ERR1(funcname,name)
#endif

-David
 
A

Artie Gold

#define ONCFILE_ERR1(funcname, 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?
Turn it inside out...

#ifdef DEBUG
#define ONCFILE_ERR1(funcname, name) \
cerr << __FILE__ << ":" << __LINE__ << " duplicated call to " \
<< funcname << " " << name << endl
#else
#define ONCFILE_ERR1(funcname, name)
#endif

HTH,
--ag
 
F

fei.liu

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?

Fei
 
A

Artie Gold

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?
No.

--ag
 
M

Michael Mair

#define ONCFILE_ERR1(funcname, 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?

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
 
C

CBFalconer

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...

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.
 

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,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top