What is the difference between these two function prototypes ?
void apply1(double(f)(double));
void apply2(double(*f)(double));
If the goal is to apply the provided function to an array, is there a version faster compared to the other one ?
EDIT :
An example of implementation :
#include <iostream>
#include <vector>
#include <cmath>
// First version
template<typename Type> void apply1(std::vector<Type>& v, Type(f)(Type))
{
for (unsigned int i = 0; i < v.size(); ++i) {
v[i] = f(v[i]);
}
}
// Second version
template<typename Type> void apply2(std::vector<Type>& v, Type(*f)(Type))
{
for (unsigned int i = 0; i < v.size(); ++i) {
v[i] = f(v[i]);
}
}
// Main
int main()
{
std::vector<double> v = {1., 2., 3., 4., 5.};
apply1(v, std::sin);
apply2(v, std::sin);
return 0;
}
First, the speed of the template wrapper instantiation is going to be almost entirely at the mercy of your optimizer.
That said, I’ve reduced your samples to the most basic code I can think of, specifically to check the invoke of the function parameters. You can read on, but you’ll see they invoke exactly the same. There is no benefit for one declaration vs another. Further, I included the one you left out, (reference-decl)
The actual asm generated from the deductions is:
apply1
apply2
apply3
They are identical (as I suspected they would be). There is no difference that I can see whatsoever.
Note: it is worth mentioning the way the compiler saw these declarations by name mangling examination: