C++ 中默认第一个参数的支持历史功能
正如这个问题重申的那样,可以为 C++ 函数中的第一个参数提供默认参数。但是
void foo(int a, int b=5); // good
void foo(int a=5, int b=5); // used to be an error
void foo(int a=5); // used to be an error
,我记得在我的编程教育早期就知道这是不允许的。这种情况从什么时候开始发生变化呢?哪个 C++ 标准消除了这个限制?由于我的大部分早期编程都是在 Visual Studio 中完成的,也许这甚至不是一个标准问题,而是一个特定的编译器限制,如果您是这样,您还记得哪个(大约)编译器版本有此限制吗?
As this question reiterates, a default parameter can be provided for the first argument in a C++ function. So the following
void foo(int a, int b=5); // good
void foo(int a=5, int b=5); // used to be an error
void foo(int a=5); // used to be an error
However, I remember learning early on in my programming education that this was not allowed. At which point did this begin to change? What C++ standard removed this limitation? Since I did most of my early programming in Visual Studio, perhaps this wasn't even a standards issue, but a specific compiler limitation, if you so, do you also remember which (approximately) compiler versions had this limitation?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你说:
从来没有这样过。你记错了。
You say:
This has never, ever been the case. You are remembering wrong.
1998 年唯一的官方 C++ 标准包含它们,因此从技术上讲,该标准一直包含它们。在此之前,甚至经常被认为是 C++ 的第一个准标准,Bjarne Stroustrup 的书 C++编程语言提到了它们。这是 Strousrup 在开发 C++ 时添加到 ANSI C 的第一件事之一。但由于该标准不是官方的,因此可能存在不支持它的编译器。
The only official c++ standard of 1998 has them, so technically speaking the standard has always had them. Before this, even the often considered first quasi-standard for C++, the Bjarne Stroustrup book The C++ Programming Language mentions them. It was one of the first things Strousrup added to ANSI C when developing C++. But as the standard wasn't official, it's possible that there were compilers out there that did not support it.
我认为您误解了您提到的问题及其答案。只要求如果第n个参数有默认值,则所有后续参数也必须有默认值。一直都是这样。从来没有任何具体规则规定 first 参数不能有默认值。请注意,默认参数是否都放在同一个声明中并不重要。下面的也OK。
I think you have misunderstood the question you referred to, and its answers. It is only required that if the nth parameter has a default value then all subsequent ones must also have default values. It has always been so. There has never been any specifiv rule that the first parameter cannot have a default value. Note that it is not important that the default parameters are all put in the same declaration. The following is also OK.
截至今天(2011 年 6 月),只有一个 C++ 标准。所以它必须符合 1998 年的 C++ 标准。
As of today (June 2011), there is only one C++ standard. So it must be in the C++ standard of 1998.