C++ void 作为函数调用的前缀。例如。 `main() {void func();}`
void func() {assert(0);}
int main () {void func();}
上面的代码没有调用func(),或者至少没有到达断言。我并不是真的需要知道,但我只是很好奇,这里发生了什么?
void func() {assert(0);}
int main () {void func();}
The above code does not call func(), or at least does not reach the assertion. Not that I really need to know, but I'm just curious, what is going on here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您正在声明一个名为
func
的函数的原型,该函数不返回任何内容,也不接受任何参数。这是函数调用和函数原型之间的细微差别(之一)。请注意,main
上面的行void func() {assert(0);}
对于这是原型还是调用没有影响。你可以删除它,代码也会做同样的事情——也就是说,什么也不做。这也告诉您可以重新声明函数原型。你甚至可以这样:
代码仍然会做以前所做的事情——什么也不做。
如果您省略
void
,它将调用该函数。另外,请注意,对于采用参数的函数,
如果您像这样在 this: 之前添加
void
,则 this: 不会变成原型:它只会产生语法错误。
You're declaring a prototype for a function named
func
which returns nothing and takes no arguments. That's (one of) the subtle difference between function calls and function prototypes. Notice that the line abovemain
, thevoid func() {assert(0);}
, has no effect on whether this is a prototype or a call. You could remove it and the code would do the same thing - that is, nothing.This also tells you that you can redeclare function prototypes. You could even have this:
And the code would still do what it did before - nothing.
If you leave off the
void
, it would call the function.Also, notice that in the case of a function which takes parameters, this:
would not turn into a prototype if you added
void
before it like this:it would just produce a syntax error.
正如其他人指出的那样,
main
内部的行被视为函数原型,而不是对函数func
的调用。在 C 和 C++ 中,如果您愿意,可以在函数内部声明函数原型,尽管在实践中很少这样做。事实上,这是合法的,这给程序员带来了各种各样的麻烦。例如,如果您将代码重写为
Then 这将编译为对
func
的调用,其返回类型显式转换为void
以指示“我不关心这个返回值。”换句话说,这组括号将声明更改为语句。在 C++ 中,这个问题可能会因为下面的代码是函数原型而不是调用默认构造函数的变量声明而变得更加复杂:
尽管
确实创建了对象并将 137 传递到其构造函数中,但
在不调用构造函数的情况下创建了对象。
该语言有一个可怕的边缘情况,称为“最令人烦恼的解析”,当尝试在调用其构造函数时声明一个对象时,就会出现这种情况。例如,这段代码是合法的 C++,但它是函数声明而不是变量声明:
问题是这可以被解析为函数声明,而不是创建接受两个临时 istream_iterator的对象。 s 作为参数。要解决这个问题,在 C++ 中,您必须编写
Where,如上所述,额外的括号强制消除语句从函数原型到声明的歧义。
希望这有帮助!
As others have pointed out, the line
inside of
main
is treated as a function prototype rather than a call to the functionfunc
. In C and C++, you can declare function prototypes inside of functions if you wish, though it's rarely done in practice.The fact that this is legal causes all sorts of headaches for programmers. For example, if you rewrote the code as
Then this would compile as a call to
func
whose return type is explicitly casted tovoid
to indicate "I don't care about this return value." In other words, this set of parentheses changes the declaration into a statement.In C++, this problem can be compounded by the fact that this code below is a function prototype, not a variable declaration invoking the default constructor:
Though
does create the object and pass 137 into its constructor, and
creates the object without calling the constructor.
There is an awful edge case of the language called the "most vexing parse" that arises when trying to declare an object while calling its constructor. For example, this code is legal C++, but it's a function declaration rather than a variable declaration:
The problem is that this could be parsed as a function declaration rather than a creation of an object that accepts two temporary
istream_iterator<int>
s as parameters. To fix this, in C++ you'd have to writeWhere, as above, the extra parentheses forcibly disambiguate the statement from being a function prototype to being a declaration.
Hope this helps!
即使没有必要,您也可以声明函数。这就是您所做的,重新声明了该函数。
You can declare functions, even when it's unnecessary. That's what you've done, re-declared the function.
您正在
main()
内声明一个本地函数void func()
。void
语句向编译器指示它是声明而不是函数调用。因此,删除void
,您的函数将被调用。You are declaring a local function
void func()
insidemain()
.The
void
statement indicates the compiler that it is a declaration and not a function call. So, remove thevoid
, your function will be called.