宏内的条件编译 [英] Conditional compilation inside a macro

查看:63
本文介绍了宏内的条件编译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我在尝试有条件地编译

宏时遇到问题。我有一个宏


#define SET_VAL(x,y)((x)= *(y))


但问题是y可以为NULL,在这种情况下我想将x设置为0.所以

我将宏修改为


#define SET_VAL(x,y)

#if y == NULL

x = 0;

#else

(x)= *(y);

#endif


这是行不通的,因为#在宏中有特殊含义。

有什么方法可以解决这个问题仍然使用宏吗?我这个
可以定义一个函数来做同样的事情,但我更喜欢使用

宏。


谢谢,

Srinivas

Hi,
I am facing a problem with trying to conditionally compile inside a
macro. I have a macro

#define SET_VAL(x, y) ((x) = *(y))

But the problem is y can be NULL in which case I want to set x to 0. So
I modified macro to

#define SET_VAL(x, y)
#if y == NULL
x = 0;
#else
(x) = *(y);
#endif

This is not working since # has a special meaning inside a macro. Is
there any way I can get around this problem with still using a macro? I
can define a function to do the same, but I would prefer to use a
macro.

Thanks,
Srinivas

推荐答案



" Srinivas Mudireddy" < SR **************** @ gmail.com>在消息中写道

news:11 ********************** @ z34g2000cwc.googlegr oups.com ...

"Srinivas Mudireddy" <sr****************@gmail.com> wrote in message
news:11**********************@z34g2000cwc.googlegr oups.com...

我在尝试有条件地在一个
宏内编译时遇到了问题。我有一个宏

#define SET_VAL(x,y)((x)= *(y))

但问题是y可以为NULL,在这种情况下我想把x设置为0.所以我修改了宏来

#define SET_VAL(x,y)
#if y == NULL
x = 0;
#else
(x)= *(y);
#endif

这是行不通的,因为#在宏中有特殊含义。是否有任何方法可以解决仍然使用宏的问题?我可以定义一个函数来做同样的事情,但我更喜欢使用
宏。
Hi,
I am facing a problem with trying to conditionally compile inside a
macro. I have a macro

#define SET_VAL(x, y) ((x) = *(y))

But the problem is y can be NULL in which case I want to set x to 0. So
I modified macro to

#define SET_VAL(x, y)
#if y == NULL
x = 0;
#else
(x) = *(y);
#endif

This is not working since # has a special meaning inside a macro. Is
there any way I can get around this problem with still using a macro? I
can define a function to do the same, but I would prefer to use a
macro.




它是否因行继续符而失败,''\''反斜杠,每行结尾

除了最后一行?


RP



Does it fail with line continuation character, ''\'' backslash, at the end of
each line, except the last?

RP

< br <>

Srinivas Mudireddy写道:
Srinivas Mudireddy wrote:

我正面临一个问题,试图在内部有条件地编译一个宏。我有一个宏

#define SET_VAL(x,y)((x)= *(y))

但问题是y可以为NULL,在这种情况下我想把x设置为
0.所以我修改了宏来

#define SET_VAL(x,y)
#if y == NULL
x = 0;
#else
(x)= *(y);
#endif

这是行不通的,因为#在宏中有特殊含义。
有什么方法可以解决这个问题仍然使用
宏?我可以定义一个函数来做同样的事情,但我更喜欢
使用宏。

I am facing a problem with trying to conditionally compile inside
a macro. I have a macro

#define SET_VAL(x, y) ((x) = *(y))

But the problem is y can be NULL in which case I want to set x to
0. So I modified macro to

#define SET_VAL(x, y)
#if y == NULL
x = 0;
#else
(x) = *(y);
#endif

This is not working since # has a special meaning inside a macro.
Is there any way I can get around this problem with still using a
macro? I can define a function to do the same, but I would prefer
to use a macro.




#define SET_VAL(x,y)\

do {\

if(y)(x)= *(y); else(x)= 0; \

}而(0)


-

"如果你想通过groups.google发布一个后续行动.com,不要使用

破碎的回复链接在文章的底部。点击

" show options"在文章的顶部,然后点击

回复在文章标题的底部。 - Keith Thompson

更多详细信息:< http://cfaj.freeshell.org/google/>

另见< http://www.safalra .com / special / googlegroupsreply />



#define SET_VAL(x, y) \
do { \
if (y) (x) = *(y); else (x) = 0; \
} while (0)

--
"If you want to post a followup via groups.google.com, don''t use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>


Srinivas Mudireddy写道:
Srinivas Mudireddy wrote:

我正面临着尝试在
宏内有条件地编译的问题。我有一个宏

#define SET_VAL(x,y)((x)= *(y))

但问题是y可以为NULL,在这种情况下我想把x设置为0.所以我修改了宏来

#define SET_VAL(x,y)
#if y == NULL
x = 0;
#else
(x)= *(y);
#endif

这是行不通的,因为#在宏中有特殊含义。是否有任何方法可以解决仍然使用宏的问题?我可以定义一个函数来做同样的事情,但我更喜欢使用
宏。
Hi,
I am facing a problem with trying to conditionally compile inside a
macro. I have a macro

#define SET_VAL(x, y) ((x) = *(y))

But the problem is y can be NULL in which case I want to set x to 0. So
I modified macro to

#define SET_VAL(x, y)
#if y == NULL
x = 0;
#else
(x) = *(y);
#endif

This is not working since # has a special meaning inside a macro. Is
there any way I can get around this problem with still using a macro? I
can define a function to do the same, but I would prefer to use a
macro.



为什么你想在地球上做这个?只需使用内联功能。


-

Ian Collins。


Why on Earth would you want to do that? Just use an inline function.

--
Ian Collins.


这篇关于宏内的条件编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆