c++ - C : MSDN says that "default arguments can be provided for pointers to functions", 但这是真的吗?

标签 c++ visual-c++ msdn default-arguments

考虑 this MSDN 文章(“默认参数”)和以下代码:

#include <stdio.h>

void print(int x) {
  printf("%d\n", x + 537);
}

typedef void (*Func)(int x = 0);

int main() {
  Func f = print;
  f();
  return 0;
}

文章声称“可以为指向函数的指针提供默认参数。例如:int (*pShowIntVal)( int i = 0 );”。据我了解,这意味着上面的代码应该可以成功编译。但是,它不会:

a.cpp
a.cpp(7) : error C2383: 'Func' : default-arguments are not allowed on this symbol
a.cpp(11) : error C2198: 'Func' : too few arguments for call

看来我误解了这篇文章。有什么意义,正确的解释是什么?

最佳答案

默认参数是函数声明的一部分,但不是其类型的一部分。所以你可以这样写:

void f(int x = 0);

但是f的类型还是

void(int x)

因此,指向函数类型的指针不能包含默认参数。

(引用:§ 8.3.6/9 [dcl.fct.default]:“默认参数不是函数类型的一部分。”)

关于c++ - C : MSDN says that "default arguments can be provided for pointers to functions", 但这是真的吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20278621/

相关文章:

c++ - g++ 在 *expected* 错误消息中给我奇怪的函数调用

c++ - C++11放弃auto_ptr的原因是什么?

c++ - @class AVPlayerLayer 的预期 unqualified-id

c++ - Sin 算法不起作用

visual-c++ - 如何解码全速率 GSM 音频文件?

c++ - 包含 minwindef.h 后的 BOOL 重新定义

windows - 在哪里报告 Windows 核心库问题?

c++ - 在 C++ 中初始化变量的偏好

licensing - 非启动微软 ISV 开发,便宜吗?

c# - 在 C# 中确定类的 default() 值