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 9235199
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:58:13+00:00 2026-06-18T06:58:13+00:00

I am working on learning c++ and I was wondering if I could pass

  • 0

I am working on learning c++ and I was wondering if I could pass in a method as a parameter in another method?

A rough outline of what I wanted to do is something like this:

void insertionSort() {
   //perform sort
}

int timeOfOperation(methodInQuestion) {
    // time 1
    methodInQuestion;
    //time 2
    return time2-time;
}

int main() {
   cout << timeOfOperation(insertionSort());
   return 0;
}

Is there a way to do something like this?

Edit:

I appreciate the replies. I have another question though. In my actual code, this is all happening in a class called dataStructure and I am creating an instance of it elsewhere and calling these methods.

dataStructure ds();
ds.timeOperation(ds.insertionSort());

When I try to implement some of the solutions posted I am getting this error:

IntelliSense: no instance of function template
"dataStructure::timeOperation" matches the argument list argument
types are: (void) object type is: dataStructure

I’m not really understanding why creating instances would affect this. Can anyone explain?

=============================================================

Edit 2:

I’m going to post more or less my exact code for this portion:

//main.cpp

#include "arrayList.h"
#include "arrayListStructure.h"
#include "Person.h"

using namespace std;

int main() {

    arrayList<Person> *al = new arrayList<Person>(length);
    arrayListStructure als(al);
    //als.fillStructure(data);
    als.timeOperation(als.insertionSort());

return 0;
}


//arrayListStructure.cpp

#include "arrayListStructure.h"
#include <functional>

double arrayListStructure::timeOperation(std::function<void()> operation) {...}
void arrayListStructure::insertionSort() {...}

arrayListStructure::arrayListStructure(arrayList<Person> *al)
{
this -> al = al;
}

There is more but I think this is all that relates to the problem

  • 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-18T06:58:15+00:00Added an answer on June 18, 2026 at 6:58 am

    Yes, something like:

    #include <functional>
    #include <ctime>
    #include <iostream>
    
    void insertionSort() { /*...*/ }
    
    std::clock_t timeOfOperation( std::function<void()> operation ) 
    {
        const std::clock_t start = std::clock();
        operation();
        return std::clock() - start;
    }
    
    int main() 
    {
        std::cout << timeOfOperation(insertionSort) << '\n';
    }
    

    Since you probably don’t want global data (which a parameterless function like insertionSort requires), you might do something like:

    template<class Container>
    void insertionSort( Container& c ) 
    { 
       /*sort the contents of c*/ 
    }
    

    Now you have to pass the data to be sorted in. You can do this with std::bind or better C++11 lambdas. Then, as noted in Yakk’s answer, we could templatize the timer function so it natively accepts std::functions, lambdas, functors (classes with operator() overloaded), or function pointers:

    template<class Operation>
    std::clock_t timeOfOperation( Operation&& operation ) 
    {/*...*/ }
    

    Here’s the full program:

    #include <functional>
    #include <iostream>
    #include <iterator>
    #include <vector>
    #include <algorithm> // for generate
    #include <ctime> // for clock
    #include <cstdlib> // for rand
    
    template<class Container>
    void insertionSort( Container& c ) 
    { 
       /*sort the contents of c*/ 
    }
    
    template<class Operation>
    std::clock_t timeOfOperation( Operation&& operation ) // or just timeOfOperation( Operation&& operation )
    {
        const std::clock_t start = std::clock();
        operation();
        return std::clock() - start;
    }
    
    int main() 
    {
        std::vector<int> v( 100 );
        std::generate( v.begin(), v.end(), std::rand );
        auto op = [&]() { insertionSort( v ); };
        std::cout << timeOfOperation( op ) << '\n';
    }
    

    Combine the last two lines (which use C++11 lambdas) as such:

    std::cout << timeOfOperation( [&]() { insertionSort( v ); } ) << '\n';
    

    But of course, in non-homework code, you should use the built-in sort functions rather than rolling your own.


    Regarding your update: The first line is actually a function definition, not an instantiation of the class:

    dataStructure ds(); // function, not an instance!
    ds.timeOperation(ds.insertionSort()); // insertionSort returns void. 
    // Can't convert void to a template param that can be called with the () operator
    

    In C++ the rule is, if something can be interpreted as a function prototype, it will be. Drop the paretheses and you’ll be fine:

    dataStructure ds; // <-- note
    ds.timeOperation(ds.insertionSort());
    

    In answer to your comment, as long as it can’t be interpreted as a prototype, you’re good. Consider:

    struct S {};
    
    struct dataStructure
    {
       dataStructure() {}
       dataStructure(int) {}
       dataStructure(S) {}
       void go() {}
    };
    
    int main()
    {
        dataStructure ds1 = dataStructure();
        dataStructure ds2(10);
        dataStructure ds3( S() ); 
        dataStructure ds4( (S()) ); // Extra parens clarify for the compiler
    
    
        ds1.go(); // Ok  
        ds2.go(); // Ok  
        ds3.go(); // Doh! ds3 is a function prototype
        ds4.go(); // Ok  
    }
    

    Update for your Edit 2:

    Change this line:

    als.timeOperation(als.insertionSort());
    

    to:

    als.timeOperation([&](){als.insertionSort()});
    

    or (less preferred, but if you don’t have lambdas):

    als.timeOperation( std::bind( &arrayListStruction::insertionSort, als ) );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working with this sample image slide page and was wondering if anyone could
I am working on a small project for learning PHP better. This project has
i have started learning something about log4j as so far its working fine here
Working through Learning Python came across factory function. This textbook example works: def maker(N):
I was wondering if any of you know any resources for learning this. I
I am working on learning in-app billing but I am having a problem with
So I'm working on learning PDO, and making the transfer from the standard PHP
Hi I'm working on learning 3d game development and I'm starting with JavaScript and
I have been working on learning the Android NDK the past few days, but
I'm working on learning Objective-C/Coaoa, but I've seem to have gotten a bit stuck

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.