The function i am writing below is made to time how long it takes to process a function.
// return type for func(pointer to func)(parameters for func), container eg.vector
clock_t timeFuction(double(*f)(vector<double>), vector<double> v)
{
auto start = clock();
f(v);
return clock() - start;
}
This is the function i want to test in my timeFunction.
template<typename T>
double standardDeviation(T v)
{
auto tempMean = mean(v); //declared else where
double sq_sum = inner_product(v.begin(), v.end(), v.begin(), 0.0);
double stdev = sqrt(sq_sum / v.size() - tempMean * tempMean);
return stdev;
}
standardDiviation was made with a template so it can accept any c++ container and i want to do the same with timeFunction, so i tried the following.
template<typename T>
clock_t timeFuction(double(*f)(T), T v)
{
auto start = clock();
f(v);
return clock() - start;
}
but this gives me errors like cannot use function template “double standardDiviation(T)” and could not deduce template argument for “overloaded function”
This is how i called the function in main.
int main()
{
static vector<double> v;
for( double i=0; i<100000; ++i )
v.push_back( i );
cout << standardDeviation(v) << endl; // this works fine
cout << timeFuction(standardDeviation,v) << endl; //this does not work
}
how do i fix the timeFunction so it works with any c++ container. Any help is greatly appreciated.
I tried to compile this code on GCC 4.7.1. It compiles and works fine. What compiler are you using? If it is not able to deduce template argument then try to specify it explicitly, i.e. use:
Also, when asking questions you should try to remove all unnecessary code.