为什么 (void) 0 在 C 和 C++ 中是无操作? [英] Why is (void) 0 a no operation in C and C++?

查看:38
本文介绍了为什么 (void) 0 在 C 和 C++ 中是无操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 glibc 中看到了 debug printfs,如果 NDEBUG 被定义,它内部定义为 (void) 0.同样,__noop Visual C++ 编译器也在那里.前者适用于 GCC 和 VC++ 编译器,而后者仅适用于 VC++.现在我们都知道,以上两条语句都将被视为无操作,不会生成相应的代码;但这是我有疑问的地方.

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.

对于__noop,MSDN 说它是编译器提供的内在函数.来到 (void) 0 ~ 为什么它被编译器解释为没有操作?它是 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?

推荐答案

(void)0 (+;) 是有效的,但无所事事"的 C++表达,仅此而已.它不会转换为目标体系结构的 no-op 指令,它只是一个空语句作为占位符,只要语言需要一个完整的语句(例如作为跳转标签的目标,或者在if 子句的主体).

(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).

(根据 Chris Lutz 的评论更新)

(updated based on Chris Lutz's comment)

需要注意的是,当用作宏时,说

It should be noted that when used as a macro, say

#define noop ((void)0)

(void) 防止它被意外地用作像

the (void) prevents it from being accidentally used as a value like

int x = noop;

对于上述表达式,编译器会正确地将其标记为无效操作.GCC 吐出 error: void value not ignored as it should be 和 VC++ 吠叫 'void' 对所有类型都是非法的.

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.

这篇关于为什么 (void) 0 在 C 和 C++ 中是无操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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