Inspired by Haskell in an elegant way to auto-generate (random) instances of a given type, for example, in QuickCheck, I'm trying to figure out how to write both an easy-to-use and a possible comparison method in C ++. I believe that I will use function templates, perhaps with the help of new functions in C ++ 11, such as variable templates. I hope that I only need to specify the function or, even better, the function template and the container type of the STL template (in turn, its value_type ), compatible with the argument (s) of the function.
I realized that benchmarking a function with a set of inputs of various sizes is somewhat similar to how threads are set up and spawned in C ++ 11. My first attempt was to copy the constructor of the thread class and turn it into a benchmark function as
template< class Function, class ...Args > inline void benchmark( Function&& f, Args&&... args );
I'm not sure if r-value refs should be used here. However, f and args must be explicitly created before the benchmark called, which leads to cumbersome non-functional use.
This results in me trying to skip the call arguments and use only the arguments instead:
namespace pnw { template <template <typename> class Function, typename Container> inline void benchmark_container() { Function<typename Container::iterator> f; Container c(10); f(c.begin(), c.end()); } }
called
typedef int T; typedef std::vector<T> C; pnw::benchmark_container<std::sort, C>();
However, now error compilation is performed as
tests/t_histogram.cpp: In function 'void test_benchmark()': tests/t_histogram.cpp:56:44: error: no matching function for call to 'benchmark_container()' tests/t_histogram.cpp:56:44: note: candidate is: tests/../benchmark.hpp:32:6: note: template<template<class> class Function, class Container> void pnw::benchmark_container()
I'm not sure if C ++ can handle the translation of a function template solely on the template arguments of another calling function.
Is this the right way to do this or is it impossible in C ++ 11? I am using GCC-4.6.