Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- template <int i, class... Types> struct VariadicTemplateElement {};
- template <int i, class T, class... Types>
- struct VariadicTemplateElement<i, T, Types...> : public VariadicTemplateElement<i - 1, Types...>{};
- template <class T, class... Types> struct VariadicTemplateElement<0, T, Types...> {
- typedef T Type;
- };
- template <int i> struct VariadicTemplateElement<i> { static_assert(i < 0, "Index is out of bounds"); };
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- template <class... Types> class Tuple;
- template <class T, class... Types> class Tuple<T, Types...> {
- public:
- Tuple() {}
- Tuple(const T& _value, const Types&... _others)
- : value(_value), others(_others...) {}
- template<int i>
- typename VariadicTemplateElement<i, T, Types...>::Type Get()
- {
- return others.Get<i - 1>();
- }
- template<>
- T Get<0>() { return value; }
- private:
- T value;
- Tuple<Types...> others;
- };
- template <> class Tuple<> {
- public:
- Tuple() {}
- };
- struct CPoint {
- int X;
- int Y;
- CPoint() : X(0), Y(0) {}
- CPoint(int x, int y) : X(x), Y(y) {}
- };
- int main()
- {
- Tuple<int, double, CPoint> result(3, 5., { 0, 2 });
- int i = result.Get<0>();
- double d = result.Get<1>();
- CPoint p = result.Get<2>();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement