fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <functional>
  4. #include <random>
  5. #include <ctime>
  6.  
  7. template<class F>
  8. struct function_traits;
  9.  
  10. template<class R, class... Args>
  11. struct function_traits<R(*)(Args...)> : public function_traits<R(Args...)>
  12. {};
  13.  
  14. template<class R, class... Args>
  15. struct function_traits<R(Args...)>
  16. {
  17. using return_type = R;
  18.  
  19. static const std::size_t arity = sizeof...(Args);
  20.  
  21. template <std::size_t N>
  22. struct argument
  23. {
  24. static_assert(N < arity, "error: invalid parameter index.");
  25. using type = typename std::tuple_element<N, std::tuple<Args...>>::type;
  26. };
  27. };
  28.  
  29. int foo(int, int) { return 0; }
  30. int bar(int, int, int, int) { return 0; }
  31.  
  32. template <typename F>
  33. void benchmark(int t, F f, const std::string& name, std::ostream& os = std::cout)
  34. {
  35. typedef typename function_traits<F>::return_type T;
  36.  
  37. std::mt19937 rng(std::time(0));
  38. std::uniform_int_distribution<T> uint_dist10(0, std::numeric_limits<T>::max());
  39.  
  40. os << "Evaluating " << name << " " << t << " times ...";
  41.  
  42. for(int i = 0; i < t; ++i)
  43. f(uint_dist10(rng), uint_dist10(rng)); // Problem!
  44.  
  45. os << " done!\n";
  46. }
  47.  
  48. int main()
  49. {
  50. benchmark(10, foo, "foo");
  51.  
  52. //Won't compile, obviously ...
  53. //benchmark(10, bar, "bar");
  54. }
Success #stdin #stdout 0s 3428KB
stdin
Standard input is empty
stdout
Evaluating foo 10 times ... done!