[英]What do the parentheses around a function name mean?
In one of my project source files, I found this C function definition: 在我的项目源文件之一中,我找到了以下C函数定义:
int (foo) (int *bar)
{
return foo (bar);
}
Note: there is no asterisk next to foo
, so it's not a function pointer. 注意:
foo
旁边没有星号,因此它不是函数指针。 Or is it? 还是? What is going on here with the recursive call?
递归调用在这里发生了什么?
In the absence of any preprocessor stuff going on, foo
's signature is equivalent to 在没有任何预处理程序发生的情况下,
foo
的签名等效于
int foo (int *bar)
The only context in which I've seen people putting seemingly unnecessary parentheses around function names is when there are both a function and a function-like macro with the same name, and the programmer wants to prevent macro expansion. 我看到人们在函数名称周围加上看似不必要的括号的唯一情况是,同时存在一个函数和一个具有相同名称的类似函数的宏,并且程序员希望防止宏扩展。
This practice may seem a little odd at first, but the C library sets a precedent by providing some macros and functions with identical names . 这种做法乍看起来似乎有些奇怪,但是C库通过提供一些具有相同名称的宏和函数来树立了先例。
One such function/macro pair is isdigit()
. 一个这样的函数/宏对是
isdigit()
。 The library might define it as follows: 该库可能将其定义如下:
/* the macro */
#define isdigit(c) ...
/* the function */
int (isdigit)(int c) /* avoid the macro through the use of parentheses */
{
return isdigit(c); /* use the macro */
}
Your function looks almost identical to the above, so I suspect this is what's going on in your code too. 您的函数看起来几乎与上面的函数相同,因此我怀疑这也是您的代码中正在发生的事情。
The parantheses don't change the declaration - it's still just defining an ordinary function called foo
. 寄生函数不会改变声明,它仍然只是定义一个称为
foo
的普通函数。
The reason that they have been used is almost certainly because there is a function-like macro called foo
defined: 使用它们的原因几乎可以肯定是因为定义了一个称为
foo
的类似于函数的宏:
#define foo(x) ...
Using (foo)
in the function declaration prevents this macro from being expanded here. 在函数声明中使用
(foo)
可防止在此处扩展此宏。 So what is likely happening is that a function foo()
is being defined with its body being expanded from the function-like macro foo
. 因此,可能发生的情况是定义了函数
foo()
,并从类似于函数的宏foo
扩展了其主体。
The parentheses are meaningless. 括号是没有意义的。
The code you show is nothing but an infinite recursion. 您显示的代码不过是无限递归。
When defining a function pointer, you sometimes see strange parentheses that do mean something. 在定义函数指针时,有时您会看到确实包含某些含义的奇怪括号。 But this isn't the case here.
但是这里不是这种情况。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.