为什么(无效)0在C和C ++无操作? [英] Why is (void) 0 a no operation in C and C++?
问题描述
我见过的的glibc 的调试用printfs在内部被定义为(无效)0
,如果 NDEBUG 的是定义。同样, __空操作
一>为Visual C ++编译器也在那儿。在GCC和VC ++编译器,前者的作品,而后者只能在VC ++。现在大家都知道,无论是上述声明将被视为无操作,也没有相应的code将产生;但这里的地方我有一个疑问。
I have seen debug printfs in glibc which internally is defined as (void) 0
, if NDEBUG is defined. Likewise the __noop
for Visual C++ compiler is there too. The former works on both GCC and VC++ compilers, while the latter only on VC++. Now we all know that both the above statements will be treated as no operation and no respective code will be generated; but here's where I've a doubt.
在的情况下,__空操作
,MSDN说,这是由编译器提供的内部函数。来到(无效)0
〜为什么间$ P $由编译器为无操作PTED?它是C语言的一个棘手的用法还是标准说的一些东西明确地?甚至可以说,是值得做的编译器实现?
In case of __noop
, MSDN says that it's a intrinsic function provided by the compiler. Coming to (void) 0
~ Why is it interpreted by the compilers as no op? Is it a tricky usage of the C language or does the standard say something about it explicity? Or even that is something to do with the compiler implementation?
推荐答案
(无效)0
(+ ;
)是合法的,但不会无的C ++前pression,这就是一切。它不翻译成目标架构的无操作
指令,它只是一个空的语句作为占位符每当语言需要一个完整的语句(例如目标为一跳转标签,或在如果
条款的机构)。
(void)0
(+;
) is a valid, but 'does-nothing' C++ expression, that's everything. It doesn't translate to the no-op
instruction of the target architecture, it's just an empty statement as placeholder whenever the language expects a complete statement (for example as target for a jump label, or in the body of an if
clause).
编辑:(根据克里斯·卢茨的评论更新)
(updated based on Chris Lutz's comment)
应当注意的是,作为宏使用时,说
It should be noted that when used as a macro, say
#define noop ((void)0)
在(无效)
$ P $被意外使用像
the (void)
prevents it from being accidentally used as a value like
int x = noop;
有关上述前pression编译器会正确地将其标记为无效操作。 GCC吐奶错误:空值不被忽略,因为它应该是
和VC ++树皮无效非法拥有各类
。
For the above expression the compiler will rightly flag it as an invalid operation. GCC spits error: void value not ignored as it ought to be
and VC++ barks 'void' illegal with all types
.
这篇关于为什么(无效)0在C和C ++无操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!