Why is (void) 0 a no operation in C and C++?(为什么 (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++ 中是无操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:为什么 (void) 0 在 C 和 C++ 中是无操作?
基础教程推荐
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 从 std::cin 读取密码 2021-01-01
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- Windows Media Foundation 录制音频 2021-01-01