By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,078 Members | 2,159 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,078 IT Pros & Developers. It's quick & easy.

is it possible to write such a macro?

P: n/a
#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?

Mar 24 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
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...

fe*****@gmail.com wrote:
#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?


Mar 24 '06 #2

P: n/a
fe*****@gmail.com wrote:
#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

Mar 24 '06 #3

P: n/a
fe*****@gmail.com wrote:
#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

--
Artie Gold -- Austin, Texas
http://goldsays.blogspot.com
"You can't KISS* unless you MISS**"
[*-Keep it simple, stupid. **-Make it simple, stupid.]
Mar 24 '06 #4

P: n/a
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
David Resnick wrote:
fe*****@gmail.com wrote:
#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


Mar 24 '06 #5

P: n/a
fe*****@gmail.com wrote:
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

--
Artie Gold -- Austin, Texas
http://goldsays.blogspot.com
"You can't KISS* unless you MISS**"
[*-Keep it simple, stupid. **-Make it simple, stupid.]
Mar 24 '06 #6

P: n/a
fe*****@gmail.com schrieb:
#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
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Mar 24 '06 #7

P: n/a
"fe*****@gmail.com" wrote:
fe*****@gmail.com wrote:

#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?


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.

--
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.
http://www.schneier.com/blog/archive...drm_rootk.html
Mar 25 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.