I'm trying to write a template that will measure the execution time of any function:
template<typename Func, typename... Args>
void measure(const Func& f, Args... args);
I have a templated function with a default parameter compare
:
template<typename T, typename Compare = std::function<bool(T, T)>>
void mergeSort(std::vector<T>& v, const Compare& compare = std::less<T>());
Then I try to measure the sort time like this:
std::vector<int> v = { 3, 5, 7, 8, 3, 24, 7, 2, 8, 0, 8 };
measure(mergeSort<int>, v);
But getting compile error: 'Func (__cdecl &)': too few arguments for call
.
Running this:
std::vector<int> v = { 3, 5, 7, 8, 3, 24, 7, 2, 8, 0, 8 };
measure(mergeSort<int>, v, std::less<int>());
all works as it should. Is there a way to make the first version workable?
Wrap the function in a lambda with a single parameter: [](auto &vec){mergeSort<int>(vec);}
.
Also, typename Compare = std::function<bool(T, T)>
is a bad idea, since the type erasure in std::function
has some overhead. You should just use std::less<T>
as the type.