C++ 中默认第一个参数的支持历史功能

发布于 2024-11-15 22:27:49 字数 411 浏览 1 评论 0原文

正如这个问题重申的那样,可以为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

绿光 2024-11-22 22:27:49

你说:

 void foo(int a=5); // used to be an error

从来没有这样过。你记错了。

You say:

 void foo(int a=5); // used to be an error

This has never, ever been the case. You are remembering wrong.

幸福%小乖 2024-11-22 22:27:49

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.

小傻瓜 2024-11-22 22:27:49

我认为您误解了您提到的问题及其答案。只要求如果第n个参数有默认值,则所有后续参数也必须有默认值。一直都是这样。从来没有任何具体规则规定 first 参数不能有默认值。请注意,默认参数是否都放在同一个声明中并不重要。下面的也OK。

void f(int x = 9, int y);
//using f here is an error
void f(int x, int y = 10);
//using f here is OK now

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.

void f(int x = 9, int y);
//using f here is an error
void f(int x, int y = 10);
//using f here is OK now
也只是曾经 2024-11-22 22:27:49

截至今天(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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文