UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

#1 2007-03-23 09:27 AM

yurec
Member
From: Singapore
Registered: 2006-11-16
Posts: 134

Re: # in macro

Hi
How can i use # in macro as symbol but not as preprocessor directive?
For example I wnat smth like this:

#define CREATE_CLASS(ClassName) #ifndef ClassName_H   \

      #define ClassName_H\

      class ClassName       \

      {                           \

      //..........                \

      }                           \

      #endif                    \


I'm not sure,that I really need it, but it's very interesting for me

Offline

#2 2007-03-23 12:22 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,829
Website

Re: # in macro

No, I don't think you can embed further preprocessor #<command>'s inside macro
definitions...  Inside a macro definition "#" takes on a new meaning: stringification of
macro args...  (Ie: takes whatever is passed as the arg and turns it into a constant
string...)  Also, even if further preprocessor commands were allowed, your example
still wouldn't work, because you need to use "##" to concatenate your macro arg
with the "_H" to form a single logical symbol...

You might want to read up on your preprocessor documentation: "man cpp" and/or
"info cpp"...

Offline

#3 2007-03-23 01:00 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,230

Re: # in macro

In the example given you'd swap it around: The ifndef in the outer, and the define in the inner place.

Offline

#4 2007-03-23 02:04 PM

yurec
Member
From: Singapore
Registered: 2006-11-16
Posts: 134

Re: # in macro

Thanks,Rob.Just wanted to show the idea.I used macros quite often,so I'm familiar with it a bit.
i3839, I don't quite understand you

Offline

#5 2007-03-23 02:48 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,230

Re: # in macro

Something like:

#ifndef ClassName_H
 #define ClassName_H
 #define CREATE_CLASS(ClassName) \
 class ClassName     \
 {                   \
 //..........        \
 }                   \
#else
 #define CREATE_CLASS(ClassName) /* empty */
#endif

Offline

#6 2007-03-23 06:17 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,829
Website

Re: # in macro

Yeah, but I got the impression that he wanted "ClassName_H" to really expand to
whatever was passed into the CREATE_CLASS() macro...  If that's NOT the case,
then the above will work...  But, if you really want to have different #ifndef/#define
stuff based on the macro arg, then I just don't think that's possible...

Offline

#7 2013-06-02 06:15 AM

dma
Member
From: Beijing China
Registered: 2013-06-02
Posts: 1
Website

Re: # in macro

The macro did not support recursive execution.

Offline

Board footer

Powered by FluxBB