在函数声明中包含参数名称与不包含参数名称有什么区别?
我记得之前读过关于在函数声明中包含参数名称和不包含参数名称之间的重要性(或缺乏重要性)的内容。但我不记得我读过什么或在哪里读过。
例如,
void do_something(int *); // No parameter name included, only type.
vs...
void do_something(int * i); // type AND parameter name included.
那么这两个声明有什么区别呢?感谢您阅读并回答这个可能微不足道的问题。
-- 更新 --
好吧,我读到的是我的一位老教授的一套风格指南,警告不要在函数定义中包含参数名称并且不要在函数中使用参数。
void do_something(int * i) { //code that doesn't use i;} //BAD
void do_something(int *) { //code that doesn't use i;} //OK
I remember reading before about the significance (or lack thereof) between including a parameter name in a function declaration and not including one. But I can't remember what it was that I read or where I read it.
for example,
void do_something(int *); // No parameter name included, only type.
vs...
void do_something(int * i); // type AND parameter name included.
So what's the difference between these two declarations? Thanks for reading and maybe answering this possibly trivial question.
-- UPDATE --
Okay, so the thing I had read was a set of style guidelines from an old professor of mine warning against including a parameter name in function definition and NOT using the parameter in the function.
void do_something(int * i) { //code that doesn't use i;} //BAD
void do_something(int *) { //code that doesn't use i;} //OK
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
就编译器而言,没有区别。
不过,添加有意义的参数名称是一种有用的文档形式。
There is no difference, as far as the compiler is concerned.
Adding meaningful parameter names is a helpful form of documentation, though.
不同之处在于,如果您选择一个好的参数名称,第二个版本可能更具可读性。没什么可说的了;-)
The difference is that the second version could be more readable if you choose a good parameter name. Nothing more to say ;-)
这些声明之间没有技术差异。如果您有
类似的描述性内容,那么这将是自我文档的改进。
There is no technical difference between those declarations. If you instead had
or something similarly descriptive, it would be an improvement in self-documentation.
在前向声明中,应该没有区别。它们都将为调用描述相同的内存布局,因此它们应该完全可以互换。
In a forward declaration, there should be no difference. They will both describe the same memory layout for the call, so they should be completely interchangeable.
对于前向声明来说,这没有任何区别。
在实际定义中,不检查没有名称的参数的使用情况。这是一种拥有未使用参数的方法,无需使用 UNUSED_PARAM(x) 宏或其他一些技巧,其唯一目的是让编译器对此关闭。
For a forward declaration, it doesn't make any difference.
In the actual definition, a parameter without a name isn't checked for use. It's a way to have unused parameters without some UNUSED_PARAM(x) macro or some other trickery whose only purpose is to shut the compiler up about it.