jamescpp

Untitled

Oct 19th, 2016
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.02 KB | None | 0 0
  1. #include <tuple>
  2.  
  3. template<typename T>
  4. struct function_traits;
  5.  
  6. template<typename R, typename ...Args>
  7. struct function_traits<R(Args...)>
  8. {
  9.     typedef R return_type;
  10.     struct args{
  11.         template <size_t i>
  12.         using type = typename std::tuple_element<i, std::tuple<Args...>>::type;
  13.         static const size_t count = sizeof...(Args);
  14.     };
  15. };
  16.  
  17.  
  18.  
  19. int f(int a){
  20.     return a;
  21. }
  22. double e(int a, double c){
  23.     return a + c;
  24. }
  25.  
  26. int main(int , char **){
  27.  
  28.     //declare a with same type as return type from f
  29.     function_traits<decltype(f)>::return_type a;
  30.  
  31.     //declare first with same type as first argument of e
  32.     //declare second with same type as second argument of e
  33.     function_traits<decltype(e)>::args::type<0> first;
  34.     function_traits<decltype(e)>::args::type<1> second;
  35.  
  36.     std::cout << "Function e have" << function_traits<decltype(e)>::args::count << " parameter/s";
  37.  
  38.  
  39.     std::cout << std::is_same< function_traits<decltype(f)>::args::type<0>, int >::value << std::endl;
  40.     return 0;
  41. }
Advertisement
Add Comment
Please, Sign In to add comment