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 - 与 C 赋值堆叠

c++ - 为什么链接到 LIB 会显着增加二进制文件的大小

c++ - 基本 OpenGL 程序崩溃,但适用于 gdb

c - 分支与多线程

c++ - 在C++中定义带有函数参数的结构体方法

c++ - 如果堆栈在数值较低的地址增长,为什么指针比较会反转它?

c++ - 从 C++ 文本文件中读取复数 (a+bi)