fork download
  1. #include <iostream>
  2. #include <string>
  3.  
  4. template <std::size_t Index, typename first, typename ...remaining>
  5. struct type_at
  6. {
  7. typedef typename type_at<Index - 1, remaining...>::type type;
  8. };
  9.  
  10. template <typename first, typename ...remaining>
  11. struct type_at<0, first, remaining...>
  12. {
  13. typedef first type;
  14. };
  15.  
  16. template <std::size_t Index, typename first, typename ...remaining>
  17. struct value_at
  18. {
  19. static typename type_at<Index, first, remaining...>::type
  20. get(first First, remaining... args)
  21. {
  22. return value_at<Index - 1, remaining...>::get(args...);
  23. }
  24. };
  25.  
  26. template <typename first, typename ...remaining>
  27. struct value_at<0, first, remaining...>
  28. {
  29. static first get(first First, remaining...)
  30. {
  31. return First;
  32. }
  33. };
  34.  
  35. template <std::size_t Index, typename T, typename ...Args>
  36. typename type_at<Index, T, Args...>::type
  37. get(T t1, Args... args)
  38. {
  39. return
  40. static_cast<type_at<Index, T, Args...>::type>
  41. (
  42. reinterpret_cast<void*>
  43. (
  44. value_at<Index, T, Args...>::get(t1, args...)
  45. )
  46. );
  47. }
  48.  
  49.  
  50. int main()
  51. {
  52. int * a = new int(10);
  53. double* b = new double(3.14);
  54. std::string c = "But I'm a string :(";
  55.  
  56. std::cout<< *get<0>(a, b, &c) <<"\n";
  57. std::cout<< *get<1>(a, b, &c) <<"\n";
  58. std::cout<< *get<2>(a, b, &c) <<"\n";
  59. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In function ‘typename type_at<Index, first, remaining>::type get(T, Args ...)’:
prog.cpp:40:15: error: expected type-specifier
   static_cast<type_at<Index, T, Args...>::type>
               ^
prog.cpp:40:15: error: expected ‘>’
prog.cpp:40:15: error: expected ‘(’
prog.cpp:46:4: error: expected ‘)’ before ‘;’ token
   );
    ^
prog.cpp: In function ‘typename type_at<Index, first, remaining>::type get(T, Args ...) [with unsigned int Index = 0u; T = int*; Args = {double*, std::basic_string<char, std::char_traits<char>, std::allocator<char> >*}; typename type_at<Index, first, remaining>::type = int*]’:
prog.cpp:47:1: warning: control reaches end of non-void function [-Wreturn-type]
 }
 ^
prog.cpp: In function ‘typename type_at<Index, first, remaining>::type get(T, Args ...) [with unsigned int Index = 1u; T = int*; Args = {double*, std::basic_string<char, std::char_traits<char>, std::allocator<char> >*}; typename type_at<Index, first, remaining>::type = double*]’:
prog.cpp:47:1: warning: control reaches end of non-void function [-Wreturn-type]
 }
 ^
prog.cpp: In function ‘typename type_at<Index, first, remaining>::type get(T, Args ...) [with unsigned int Index = 2u; T = int*; Args = {double*, std::basic_string<char, std::char_traits<char>, std::allocator<char> >*}; typename type_at<Index, first, remaining>::type = std::basic_string<char>*]’:
prog.cpp:47:1: warning: control reaches end of non-void function [-Wreturn-type]
 }
 ^
stdout
Standard output is empty