函数的未命名参数可以具有默认值吗? [英] Can an unnamed parameter of function have a default value?

查看:123
本文介绍了函数的未命名参数可以具有默认值吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码在C ++中合法吗?

Is the following code legal in C++?

void f(void* = 0)
{}

int main()
{
    f();
}

哪个C ++标准页面指出此用法合法?

Which page of the C++ standard states that this usage is legal?

推荐答案

是的,这是合法的。

有没有标准的措辞来明确允许这些功能组合;

There is no standard wording to allow this combination of features specifically; there simply isn't any to disallow it, either.

默认参数语法适用于 parameter-declaration


[C ++ 11:8.3.6 / 1]: 如果在 parameter-declaration 中指定了 initializer-clause ,则此 initializer-clause 用作默认参数。默认参数将用于缺少尾部参数的调用中。

[C++11: 8.3.6/1]: If an initializer-clause is specified in a parameter-declaration this initializer-clause is used as a default argument. Default arguments will be used in calls where trailing arguments are missing.

...以及中的函数参数参数声明可能未命名:

...and function parameters in a parameter-declaration may be unnamed:


[C ++ 11:8.3.5 / 11 ]: [..] 可以可选地提供标识符作为参数名称。 [..]

[C++11: 8.3.5/11]: [..] An identifier can optionally be provided as a parameter name. [..]

甚至在8.3.6 / 4下也有这种用法的示例(尽管示例不是规范性文本,因此不能用于具体证明任何内容。

There is even an example of this usage under 8.3.6/4 (though examples are not normative text, so this cannot be used to prove anything concretely).

这篇关于函数的未命名参数可以具有默认值吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆