Advertisement
Guest User

Untitled

a guest
Apr 24th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.43 KB | None | 0 0
  1. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2. template <int i, class... Types> struct VariadicTemplateElement {};
  3.  
  4. template <int i, class T, class... Types>
  5. struct VariadicTemplateElement<i, T, Types...> : public VariadicTemplateElement<i - 1, Types...>{};
  6.  
  7. template <class T, class... Types> struct VariadicTemplateElement<0, T, Types...> {
  8.     typedef T Type;
  9. };
  10.  
  11. template <int i> struct VariadicTemplateElement<i> { static_assert(i < 0, "Index is out of bounds"); };
  12.  
  13.  
  14.  
  15. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  16. template <class... Types> class Tuple;
  17.  
  18. template <class T, class... Types> class Tuple<T, Types...> {
  19. public:
  20.     Tuple() {}
  21.     Tuple(const T& _value, const Types&... _others)
  22.         : value(_value), others(_others...) {}
  23.  
  24.     template<int i>
  25.     typename VariadicTemplateElement<i, T, Types...>::Type Get()
  26.     {
  27.         return others.Get<i - 1>();
  28.     }
  29.  
  30.     template<>
  31.     T Get<0>() { return value; }
  32.  
  33. private:
  34.     T value;
  35.     Tuple<Types...> others;
  36. };
  37.  
  38. template <> class Tuple<> {
  39. public:
  40.     Tuple() {}
  41. };
  42.  
  43. struct CPoint {
  44.     int X;
  45.     int Y;
  46.  
  47.     CPoint() : X(0), Y(0) {}
  48.     CPoint(int x, int y) : X(x), Y(y) {}
  49. };
  50.  
  51. int main()
  52. {
  53.     Tuple<int, double, CPoint> result(3, 5., { 0, 2 });
  54.  
  55.     int i = result.Get<0>();
  56.     double d = result.Get<1>();
  57.     CPoint p = result.Get<2>();
  58.  
  59.     return 0;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement