问题描述
以下是完全合法的C++代码
void foo (int) {
cout << "Yo!" << endl;
}
int main (int argc, char const *argv[]) {
foo(5);
return 0;
}
我想知道,考虑到无法从函数内部引用它们,是否存在将未命名参数留在函数中的值.
I wonder, if there a value to ever leave unnamed parameters in functions, given the fact that they can't be referenced from within the function.
为什么这首先是合法的?
Why is this legal to begin with?
推荐答案
是的,这是合法的.这对于在不打算使用相应参数的实现中从基类实现虚拟非常有用:您必须声明参数以匹配基类中的虚拟函数的签名,但您不打算使用它,所以你不用指定名称.
Yes, this is legal. This is useful for implementations of virtuals from the base class in implementations that do not intend on using the corresponding parameter: you must declare the parameter to match the signature of the virtual function in the base class, but you are not planning to use it, so you do not specify the name.
另一种常见情况是当您向某个库提供回调时,您必须符合该库已建立的签名(谢谢,Aasmund Eldhuset 提出这个问题.
The other common case is when you provide a callback to some library, and you must conform to a signature that the library has established (thanks, Aasmund Eldhuset for bringing this up).
还有一个特殊情况 定义您自己的后递增和后递减运算符:它们必须具有带有 int 参数的签名,但该参数始终未使用.不过,这种约定接近于语言设计中的一种黑客行为.
There is also a special case for defining your own post-increment and post-decrement operators: they must have a signature with an int parameter, but that parameter is always unused. This convention is bordering on a hack in the language design, though.
这篇关于为什么 C++ 允许未命名的函数参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!


大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)