c++ - 不带星号的函数指针参数

标签 c++ c function-pointers function-declaration

我见过这个函数的定义,它接收一个函数指针作为参数:

double fin_diff(double f(double), double x, double h  = 0.01) {
  return (f(x+h)-f(x)) / h;
}

我习惯用星号看到这个定义,即:

double fin_diff(double (*f)(double), double x, double h  = 0.01);

你知道为什么第一个定义也有效吗?

最佳答案

标准说这两个函数是等价的,因为函数参数被调整为指向函数参数的指针:

16.1 Overloadable declarations [over.load]
(3.3) Parameter declarations that differ only in that one is a function type and the other is a pointer to the same function type are equivalent. That is, the function type is adjusted to become a pointer to function type (11.3.5).

在 C 中相同:

6.7.5.3 Function declarators (including prototypes)
8 A declaration of a parameter as ‘‘function returning type’’ shall be adjusted to ‘‘pointer to function returning type’’, as in 6.3.2.1.

关于c++ - 不带星号的函数指针参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57143586/

相关文章:

c - C中程序的错误答案(中缀符号->后缀符号)

c - 尽管数组元素数量众多,但函数中数组的c sizeof函数始终为四个字节

c - 在内存中执行机器代码

c++ - 为什么我的回调没有被调用?

c - (C语言)检查字符串中的元音

c++ - 从我的 mac 上的文件将 g++ 安装到终端

c++ - 如何在 C 或 C++ 中编写简单的正则表达式模式匹配函数?

c++ - 使用指向函数指针的外部引用时的链接器警告

c++ - 如何从 CV_8UC4 矩阵中获取值

c++ - 如何调试奇怪的内存泄漏 (C++)