Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9116679
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T04:43:58+00:00 2026-06-17T04:43:58+00:00

The function i am writing below is made to time how long it takes

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T04:43:59+00:00Added an answer on June 17, 2026 at 4:43 am

    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:

    cout << timeFuction(standardDeviation< vector<double> >,v) << endl;
    

    Also, when asking questions you should try to remove all unnecessary code.

    #include <iostream>
    #include <vector>
    
    using namespace std;
    
    template<typename T>
    double standardDeviation(T v)
    {
        return 5;
    }
    
    template<typename T>
    int timeFuction(double(*f)(T), T v)
    {
       // auto start = clock();
        f(v);
        return 0;//clock() - start;
    }
    
    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 also work
    
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing below function to return Suffix passing name as parameter. I made
I'm writing a function that takes two parameters, the first being the id of
I'm writing a function that replaces long hex coded color ( #334455 ) with
I am writing an asynchronous class to simplify function operation. Below is my code:
I am writing a function inListi() which takes at least one argument and compares
Hey I am writing a function that takes a matrix input such as the
I am writing a function that returns a table. There are two parameters that
i am writing a function in javascript that will return date array of all
I am writing function that involve other function from base R with a lot
I'm writing a function, which determine the number of useful bits of a 16

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.