宏和副作用 [英] macros and side effects
问题描述
C宏是否与侧面效果相结合是不安全的,即使它们是干净的,也是干净的。例如可以
foo_macro(++ c);
执行++ c多次,即使foo_macro是干净的?我总是认为干净是干净的。宏(即宏中所有参数都是定义中括号括起来的
)没有这个问题...
我想知道因为我有在参考文献中查找了putc(),并且它表示如果通过的话,它是不安全的。值有副作用,我假设
标准宏是干净的......
>>>>" C" == copx< co ** @ gazeta.plwrites:
c如果C与宏结合使用是不安全的,那么即使它们是b
"清洁"例如可以
cfoo_macro(++ c);
#define foo_macro(x)(x)*(x)
< br $> b $ b哎呀。
Charlton
-
Charlton Wilbur
cw*****@chromatico.net
copx写道:
如果C宏与侧面效果相结合是不安全的,即使
它们是干净的例如可以
foo_macro(++ c);
执行++ c多次,即使foo_macro是干净的?我总是认为干净是干净的。宏(即宏中所有参数都是定义中括号括起来的
)没有这个问题...
我想知道因为我有在参考文献中查找了putc(),并且它表示如果通过的话,它是不安全的。值有副作用,我假设
标准宏是干净的...
你的引用是正确的,并且 ;清洁"宏可以多次评估他们的
参数。举个简单的例子,如果你有
#define foo_macro(x)((x)*(x))
那么foo_macro'的参数是完全括号的,但是foo_macro(++ c)
仍将扩展为((++ c)*(++ c))。
对于大多数标准库函数,如果它们也是作为一个宏实现的,那么它必须是宏,那个宏只计算每个参数
一次。 putc是一个例外。
copx说:
C宏是不是真的不安全
如果写得正确并且使用不正确,则不会。
与副作用结合使用即使它们是"清洁"例如可以
foo_macro(++ c);
执行++ c多次,即使foo_macro是干净的?
这不是一个广泛用于描述宏的术语。 (翻译:这是我第一次看到它时的
。)
我有
总是
认为干净宏(即宏中所有参数都是定义中用圆括号括起来的
)没有这个
问题...
你认为错了。
我想知道因为我刚刚在参考文献中查找了putc(),
和它说,如果通过它是不安全的。值有副作用,
你的引用说错了。 putc函数,如果实现为宏,
可以多次评估流,但*不是*字符。并且
你很难把它称为putc(''\ n'',fp ++),我相信吗?
和我假设标准宏是干净的...
为什么?
-
Richard Heathfield
Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk
电子邮件:rjh在上述域名中, - www。
Is it true that C macros are unsafe when combined with side effects even if
they are "clean" e.g. could
foo_macro(++c);
execute "++c" multiple times even if foo_macro is "clean"? I have always
thought that "clean" macros (i.e. macros where all parameters are
surrounded by parentheses in the definition) do not have this problem...
I am wondering because I have just looked up putc() in a reference, and it
says that it is unsafe if the "passed" value has a side effect, and I assume
standard macros are "clean"...
>>>>"c" == copx <co**@gazeta.plwrites:
cIs it true that C macros are unsafe when combined with side
ceffects even if they are "clean" e.g. could
cfoo_macro(++c);
#define foo_macro(x) (x)*(x)
Whoops.
Charlton
--
Charlton Wilbur
cw*****@chromatico.net
copx wrote:Is it true that C macros are unsafe when combined with side effects even if
they are "clean" e.g. could
foo_macro(++c);
execute "++c" multiple times even if foo_macro is "clean"? I have always
thought that "clean" macros (i.e. macros where all parameters are
surrounded by parentheses in the definition) do not have this problem...
I am wondering because I have just looked up putc() in a reference, and it
says that it is unsafe if the "passed" value has a side effect, and I assume
standard macros are "clean"...Your reference is correct, and "clean" macros can evaluate their
arguments multiple times. As a simple example, if you have
#define foo_macro(x) ((x) * (x))
then foo_macro''s arguments are fully parenthesised, but foo_macro(++c)
will still expand to ((++c) * (++c)).
For most standard library functions, it is required that if they are
also implemented as a macro, that that macro evaluates each argument
exactly once. putc is an exception.
copx said:
Is it true that C macros are unsafeNot if written correctly and used correctly.
when combined with side effects even if they are "clean" e.g. could
foo_macro(++c);
execute "++c" multiple times even if foo_macro is "clean"?That is not a widely-used term for describing macros. (Translation: it''s
the first time I have ever seen it.)
I have
always
thought that "clean" macros (i.e. macros where all parameters are
surrounded by parentheses in the definition) do not have this
problem...You thought wrong.
I am wondering because I have just looked up putc() in a reference,
and it says that it is unsafe if the "passed" value has a side effect,Your reference says wrong. The putc function, if implemented as a macro,
may evaluate the stream more than once, but *not* the character. And
you''re hardly likely to call it as putc(''\n'', fp++), I trust?
and I assume standard macros are "clean"...Why?
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
这篇关于宏和副作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!