gpt4 book ai didi

c++ - 函数的未命名参数可以有默认值吗?

转载 作者:可可西里 更新时间:2023-11-01 14:56:01 27 4
gpt4 key购买 nike

以下代码在 C++ 中是否合法?

void f(void* = 0)
{}

int main()
{
f();
}

C++ 标准的哪一页声明这种用法是合法的?

最佳答案

是的,这是合法的。

没有标准的措辞来专门允许这种功能组合;也没有什么可以禁止的。

默认参数语法适用于参数声明中的函数参数:

[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.

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

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

在8.3.6/4下甚至有这种用法的例子(虽然例子不是规范文本,所以不能用来具体证明什么)。

关于c++ - 函数的未命名参数可以有默认值吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14675908/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com