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

标签 c++ function c++11 default-parameters function-signature

以下代码在 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/

相关文章:

c++ - 可以在主体中调用 C++ 中的委托(delegate)构造函数还是仅在初始化列表中调用?

c - Windows FTP 函数 [C]

c++ - 如何允许最终用户使用正则表达式创建验证规则?

visual-studio-2010 - 函数声明中的 static_assert

c++ - 模板 模板参数

c++ - 使用 chrono::high_resolution_clock::now() 的奇怪行为

javascript - 如何制作一个可重用的函数,以便我保持 DRY 的良好实践?

javascript - 使用 Applescript 从外部文件调用 Javascript 函数

c++ - 重载所有基本整数类型是否足以捕获所有整数?

c++ - 构造函数的继承错误调用