Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > is it possible to write such a macro?

Reply
Thread Tools

is it possible to write such a macro?

 
 
fei.liu@gmail.com
Guest
Posts: n/a
 
      03-24-2006
#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?

 
Reply With Quote
 
 
 
 
fei.liu@gmail.com
Guest
Posts: n/a
 
      03-24-2006
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...

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?


 
Reply With Quote
 
 
 
 
David Resnick
Guest
Posts: n/a
 
      03-24-2006
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

 
Reply With Quote
 
Artie Gold
Guest
Posts: n/a
 
      03-24-2006
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.]
 
Reply With Quote
 
fei.liu@gmail.com
Guest
Posts: n/a
 
      03-24-2006
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:
> 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


 
Reply With Quote
 
Artie Gold
Guest
Posts: n/a
 
      03-24-2006
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.]
 
Reply With Quote
 
Michael Mair
Guest
Posts: n/a
 
      03-24-2006
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.
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      03-25-2006
"" wrote:
> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Such a pitty! Such a great person gone! WhorryIrressy Wireless Networking 0 01-28-2008 04:24 PM
How to write game such like this? Hugo C++ 1 04-01-2006 06:04 PM
cannot open file in write mode, no such file or directory haynesc@gmail.com Python 6 03-01-2005 04:17 PM
write a function such that when ever i call this function in some other function .it should give me tha data type and value of calling function parameter komal C++ 6 01-25-2005 11:13 AM
How to write such a function? tings C++ 6 01-09-2005 10:36 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57