Created
September 11, 2021 14:20
-
-
Save ink19/557bcbeb5a26a96224e18d6f39f26b20 to your computer and use it in GitHub Desktop.
tuple impl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <tuple> | |
// 获取第N个元素的类型 | |
template <int N, typename ...S> | |
class tuple_item; | |
template <int N, typename TS, typename ...S> | |
class tuple_item<N, TS, S...>: public tuple_item<N, S...> {}; | |
template <typename TS, typename ...S> | |
class tuple_item<sizeof...(S) + 1, TS, S...> { | |
public: | |
typedef TS type; | |
}; | |
template<int N, typename TS, typename ...S> | |
class tuple_work : public tuple_work<N, S...>{}; | |
template<typename TS, typename ...S> | |
class tuple_work <sizeof...(S) + 1, TS, S...> : public tuple_work<sizeof...(S), S...> { | |
public: | |
tuple_work(TS &&arg1, S&& ...args) : | |
_data(std::forward<TS>(arg1)), | |
tuple_work<sizeof...(S), S...>(std::forward<S>(args) ...) { | |
} | |
static TS get(tuple_work<sizeof...(S) + 1, TS, S...> &&t) { | |
return std::forward<tuple_work<sizeof...(S) + 1, TS, S...>>(t)._get(); | |
} | |
TS _get() { | |
return _data; | |
} | |
private: | |
TS &&_data; | |
}; | |
template<typename TS> | |
class tuple_work<1, TS> { | |
public: | |
tuple_work(TS &&arg1) : _data(std::forward<TS>(arg1)) {} | |
static TS get(tuple_work<1, TS> &&t) { | |
return std::forward<tuple_work<1, TS>>(t)._get(); | |
} | |
TS _get() { | |
return _data; | |
} | |
private: | |
TS &&_data; | |
}; | |
template<typename ...S> | |
class tuple : public tuple_work<sizeof...(S), S...>{ | |
public: | |
tuple(S&& ...args) : tuple_work<sizeof...(S), S...>(std::forward<S>(args)...) {} | |
}; | |
template<int N, typename S> | |
class tuple_get_work; | |
template<int N, typename ...S> | |
class tuple_get_work<N, tuple<S...>> { | |
public: | |
static typename tuple_item<N, S...>::type get(tuple<S...> &&data) { | |
return tuple_work<N, S...>::get(std::forward<tuple<S...>>(data)); | |
} | |
}; | |
template<int N, typename ...S> | |
typename tuple_item<sizeof...(S) - N, S...>::type get(tuple<S...> &&data) { | |
return tuple_get_work<sizeof...(S) - N, tuple<S...>>::get(std::forward<tuple<S...>>(data)); | |
} | |
int main() { | |
auto data = tuple<int, int, int>(123, 234, 1234); | |
std::cout << get<0>(tuple<double, int, std::string>(123.1, 234, "1234")) << std::endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment