问题描述
这个程序如何编译得很好?
How does this program get compiled fine?
int main() {
void(); // Does this create a "void" object here?
}
我已经在MSVC和GCC下测试过。但 void
是不完整的类型。当您对任何其他不完整的用户定义类型执行相同操作时,
I've tested both under MSVC and GCC. But void
is an incomplete type. When you do the same for any other incomplete user-defined type,
class Incomplete;
int main() {
Incomplete(); // Error saying "Incomplete" is incomplete.
}
推荐答案
void
类型是且始终是特殊的。它确实是不完整的,但是在许多情况下是允许的,其中通常期望完整类型。否则,例如,由于 void
类型的不完整性, void
函数的定义将无效。也可以编写 void
类型的表达式(对 void
函数的任何调用都是这种表达式的示例) 。
void
type is and has always been special. It is indeed incomplete, but it is allowed in many contexts where a complete type is typically expected. Otherwise, for one example, a definition of a void
function would be invalid because of incompleteness of void
type. It is also possible to write expressions of void
type (any call to a void
function is an example of such expression).
即使在C语言中,也可以使用 void
的直接表达式)0 。你在代码中有什么只是一个C ++特定的语法的例子,它基本上是一样的事情:它产生类型 void
的无操作表达式。
Even in C language you can use immediate expressions of void
type like (void) 0
. What you have in your code is just an example of C++-specific syntax that does essentially the same thing: it produces a no-op expression of type void
.
这篇关于什么是“void();”作为一个单独的语句意味着C ++?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!