How to declare a function that takes a function with a function as an argument?

c++, function-pointers, time

Solution

A simple solution:

template<typename T>
auto runWithTime0(T _func) -> decltype(_func())
{
  startTimer();
  _func();
  endTimer();
}

template<typename T, typename P1>
auto runWithTime1(T _func, P1 _arg1) -> decltype(_func(_arg1))
{
  startTimer();
  _func(_arg1);
  endTimer();
}

// ...etc

You can do something similar with boost::bind and what not as well, but if that's not available the above should do the trick.

Edit: added return value, which will work if your compiler supports c++11 (VC2010/2012, g++4.7 or higher I believe)

Problem

Sorry for the long-winded and confusing title! Here's my problem: I'm trying to write a function to output the time that another function takes. Normally I'd just pass in the function and its arguments but in this instance, the functions I'm trying to time themselves take functions as arguments. For a concrete example, I'm trying to get this to work: ``` void foo(void (*f) (T*)){ ...function stuff... } --------not sure what this should be | void runWithTime(void (*f) (void (*g) (T*))){ f(g) } //runWithTime(foo); ``` I want to be able to call `runWithTime(foo)`, but I'm not sure what the type `runWithTime`'s argument should be. Any help would be great! Thanks in advance.

Original source