功能一样的宏 [英] Function-like macro
问题描述
如何让if / then / else宏作为函数使用
以便整个事情看起来像返回值?
我为初学者尝试了这个蹩脚的尝试:
#define A_FROM_B(b)\
( \
if(b< 10){\
a = b; \
} \
else {\
a = 2 * b; \
},\
a; \
)
Russell Shaw< rjshawN_o@s_pam.netspace.net.au>写道:
我如何使一个if / then / else宏作为一个函数
,以便整个事情看起来像返回值?
你没有。这就是?:操作员的用途。
我为初学者尝试了这个蹩脚的尝试:
#define A_FROM_B(b)\
(\\ \\ n \\ n如果(b <10){\
a = b;
a?这是什么?如果我在一个函数中调用此宏会发生什么? br $>
a struct?
} \
其他{\
a = 2 * b; \
},\
a; \
)
#define A_FROM_B(b)((b)< 10?(b):2 *(b) )
注意宏定义中b周围的parens。有一天,当你决定拨打A_FROM_B(x + 10)时,他们将节省你的培根。
Richard
Richard Bos写道:Russell Shaw< rjshawN_o@s_pam.netspace.net.au>写道:
我如何使一个if / then / else宏作为一个函数
使整个事物看起来像返回值?
你不是。这就是?:运算符的用途。
我尝试了这个对初学者的蹩脚尝试:
#define A_FROM_B(b)\
(\
if(b< 10){\
a = b; \
a?什么是?如果发生了什么?我在一个函数中调用这个宏是一个结构吗?
就像我说的那样,它是一个蹩脚的;)
< blockquote class =post_quotes>} \
else {\
a = 2 * b; \
},\
一个; \
)
#define A_FROM_B(b)((b)< 10?(b):2 *(b))
>注意宏定义中b周围的parens。有一天当你决定打电话给A_FROM_B(x + 10)时,他们会保存你的培根。
理查德
做什么我做的事情包含更复杂的陈述,例如:
if(size> 0x1ff){
size_t sz = size;
for(ndx = 0; ndx< 32; ndx ++){
if(sz< = 0){
break;
}
sz>> = 1;
}
ndx - = 8;
ndx< ;< = 9;
}
else {
ndx = size;
}
我必须把它变成一个函数吗?
2005年5月24日星期二20:24:28 +1000,>
Russell Shaw< rjshawN_o@s_pam.netspace.net.au>写道:
我必须把它变成一个函数吗?
内联如果可用的话,函数不会是一个糟糕的选择
与您的编译器。否则,do {...} while(0)制作像宏这样的函数是一种常见的
的理念。
Villy
Hi,
How do i make an if/then/else macro act as a function
so that the whole thing looks like the return value?
I tried this lame attempt for starters:
#define A_FROM_B(b) \
( \
if(b < 10) { \
a = b; \
} \
else { \
a = 2*b; \
}, \
a; \
)
Russell Shaw <rjshawN_o@s_pam.netspace.net.au> wrote:
How do i make an if/then/else macro act as a function
so that the whole thing looks like the return value?
You don''t. That''s what the ?: operator is for.
I tried this lame attempt for starters:
#define A_FROM_B(b) \
( \
if(b < 10) { \
a = b;
a? What is a? What happens if I call this macro in a function where a is
a struct?
} \
else { \
a = 2*b; \
}, \
a; \
)
#define A_FROM_B(b) ((b)<10? (b): 2*(b))
Note the parens around b in the definition of the macro. They will save
your bacon some day when you decide to call A_FROM_B(x+10).
Richard
Richard Bos wrote:Russell Shaw <rjshawN_o@s_pam.netspace.net.au> wrote:How do i make an if/then/else macro act as a function
so that the whole thing looks like the return value?
You don''t. That''s what the ?: operator is for.I tried this lame attempt for starters:
#define A_FROM_B(b) \
( \
if(b < 10) { \
a = b; \
a? What is a? What happens if I call this macro in a function where a is
a struct?
Like i said, it''s a lame one;)
} \
else { \
a = 2*b; \
}, \
a; \
)
#define A_FROM_B(b) ((b)<10? (b): 2*(b))
Note the parens around b in the definition of the macro. They will save
your bacon some day when you decide to call A_FROM_B(x+10).
Richard
What do i do with things that contain more complex statements like:
if(size > 0x1ff) {
size_t sz = size;
for(ndx = 0; ndx < 32; ndx++) {
if(sz <= 0) {
break;
}
sz >>= 1;
}
ndx -= 8;
ndx <<= 9;
}
else {
ndx = size;
}
Do i have to make it into a function?
On Tue, 24 May 2005 20:24:28 +1000,
Russell Shaw <rjshawN_o@s_pam.netspace.net.au> wrote:
Do i have to make it into a function?
An inline function wouldn''t be such a bad alternative if it is available
with your compiler. Otherwise, "do { ... } while (0)" is a common
ideom for making a function like macro.
Villy
这篇关于功能一样的宏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!