参数名称省略,C++与C
在 C++ 中,在某些情况下我倾向于省略参数名称。但在 C 中,当我省略参数名称时出现错误。
这是代码:
void foo(int); //forward-decl, it's OK to omit the parameter's name, in both C++ and C
int main()
{
foo(0);
return 0;
}
void foo(int) //definition in C, it cannot compile with gcc
{
printf("in foo\n");
}
void foo(int) //definition in C++, it can compile with g++
{
cout << "in foo" << endl;
}
这是为什么? C 函数定义中不能省略参数名称吗?
In C++, I tend to omit the parameter's name under some circumstances. But in C, I got an error when I omitted the parameter's name.
Here is the code:
void foo(int); //forward-decl, it's OK to omit the parameter's name, in both C++ and C
int main()
{
foo(0);
return 0;
}
void foo(int) //definition in C, it cannot compile with gcc
{
printf("in foo\n");
}
void foo(int) //definition in C++, it can compile with g++
{
cout << "in foo" << endl;
}
Why is that? Can't I omit the parameter's name in C function definition?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不可以,在 C 语言中,您不能省略函数定义中参数的标识符。
C99标准说:
C++14 标准规定:
No, in C you cannot omit identifiers for parameters in function definitions.
The C99 standard says:
The C++14 standard says:
原因是各自的语言标准都是这么说的,但差异是有原因的。
如果您不提供参数名称,则该函数无法引用该参数。
在 C 中,如果函数忽略其参数之一,通常只需将其从声明和定义中删除,而不在任何调用中传递它,通常是有意义的。回调函数可能是一个例外,其中函数的集合必须具有相同的类型,但并非所有函数都必须使用其参数。但这并不是很常见的情况。
在 C++ 中,如果函数派生自某个父类中定义的函数,则它必须具有与父类相同的签名,即使子函数不使用参数值之一。
(请注意,这与默认参数无关;如果 C++ 中的参数有默认值,则调用者不必显式传递它,但函数定义如果要引用它,仍然必须提供名称。 )
更新:C 标准的下一版本(C23、ISO/IEC 9899:2023 (E))很可能将允许省略参数名称。
The reason is that that's what the respective language standards say, but there is a rationale for the difference.
If you don't provide a name for a parameter, then the function cannot refer to that parameter.
In C, if a function ignores one of its parameters, it usually makes sense just to remove it from the declaration and the definition, and not pass it in any calls. An exception might be a callback function, where a collection of functions all have to be of the same type but not all of them necessarily use their parameters. But that's not a very common scenario.
In C++, if the function is derived from a function defined in some parent class, it has to have the same signature as the parent, even if the child function has no use for one of the parameter values.
(Note that this is not related to default parameters; if a parameter in C++ has a default value, the caller doesn't have to pass it explicitly, but the function definition still has to provide a name if it's going to refer to it.)
UPDATE: It's likely that the next edition of the C standard (C23, ISO/IEC 9899:2023 (E)) will allow parameter names to be omitted.
在纯粹的实践层面上,我每天都在处理这个问题。迄今为止最好的解决方案是使用预处理器。我的常见头文件包含:
使用 UNUSED 的一个示例是:
有时您实际上需要引用该参数,例如在断言()或调试语句中。您可以通过以下方式执行此操作:
另外,以下内容也很有用:
示例:
和:
On a purely practical level, I have deal with this daily. The best solution to date is to use the pre-processor. My common header file contains:
An example of the use of UNUSED is:
Sometimes you actually need to refer to the parameter, for example in an assert() or debug statement. You can do so via:
Also, the following are useful:
Examples:
and:
函数原型中可以省略参数名称,但必须在函数实现中声明。例如,在 GCC 4.6.1 下编译和运行得很好
输出:
In foo with value 10 and 15!
至于为什么(除了因为标准这么说):C++ 允许你调用函数不使用所有参数,而 C 则不然。如果您没有为 C 中的函数提供所有参数,则编译器将抛出
错误:函数'foo'的参数太少
You can omit the parameter name in the function prototype, but you must declare it in the function implementation. For example, this compiles and runs just fine under GCC 4.6.1
Outputs:
In foo with value 10 and 15!
As to why (other than because the standards say so): C++ allows you to call a function without using all of the arguments, while C doesn't. If you don't supply all the arguments to a function in C, then the compiler will throw
error: too few arguments to function 'foo'