我见过这个函数的定义,它接收一个函数指针作为参数:
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/