C++实现的Tuple类,非常好用,新老编译器都支持。在编译器支持可变模版参数的情况下,这个Tuple支持任意多成员,不支持的情况下,支持的成员是有限的,现在只支持3个成员,需要支持跟多成员,也可以轻松扩展下。有码有真相:
- #include <iostream>
- #include <typeinfo>
- using namespace std;
- #ifdef __GXX_EXPERIMENTAL_CXX0X__
- template <typename... Rest> struct Tuple;
- template <> struct Tuple<> {};
- template <typename First, typename ... Rest>
- struct Tuple<First, Rest...> : public Tuple<Rest...> {
- Tuple() : value() {}
- Tuple(First &&first, Rest&&... rest)
- : value(std::forward<First>(first))
- , Tuple<Rest...>(std::forward<Rest>(rest)...)
- {}
- First value;
- };
- template <size_t N, typename TP> struct Tuple_Element;
- template <typename T, typename ... Rest>
- struct Tuple_Element<0, Tuple<T, Rest...>> {
- typedef T type;
- typedef Tuple<T, Rest...> TPType;
- };
- template <size_t N, typename T, typename ... Rest>
- struct Tuple_Element<N, Tuple<T, Rest...>>
- : public Tuple_Element<N - 1, Tuple<Rest...>>
- {};
- template <size_t N, typename ... Rest>
- typename Tuple_Element<N, Tuple<Rest...>>::type& get(Tuple<Rest...> &tp) {
- typedef typename Tuple_Element<N, Tuple<Rest...>>::TPType type;
- return ((type &)tp).value;
- }
- #else
- struct VoidType;
- template <typename T1, typename T2, typename T3>
- struct Tuple;
- template <>
- struct Tuple<VoidType, VoidType, VoidType> {
- };
- template <typename T1>
- struct Tuple<T1, VoidType, VoidType> {
- Tuple()
- {}
- Tuple(const T1 &v1)
- : value(v1)
- {}
- T1 value;
- };
- template <typename T1, typename T2>
- struct Tuple<T1, T2, VoidType>
- : public Tuple<T2, VoidType, VoidType>
- {
- Tuple()
- {}
- Tuple(const T1 &v1, const T2 &v2)
- : value(v1)
- , Tuple<T2, VoidType, VoidType>(v2)
- {}
- T1 value;
- };
- template <typename T1 = VoidType, typename T2 = VoidType, typename T3 = VoidType>
- struct Tuple
- : public Tuple<T2, T3, VoidType>
- {
- Tuple(const T1 &v1, const T2 &v2, const T3 &v3)
- : value(v1)
- , Tuple<T2, T3, VoidType>(v2, v3)
- {}
- T1 value;
- };
- template <size_t N, typename T1, typename T2, typename T3>
- struct Tuple_Element;
- template <>
- struct Tuple_Element<0, VoidType, VoidType, VoidType> {
- };
- template <typename T1, typename T2, typename T3>
- struct Tuple_Element<0, T1, T2, T3> {
- typedef T1 type;
- typedef Tuple<T1, T2, T3> TPType;
- };
- template <size_t N, typename T1, typename T2, typename T3>
- struct Tuple_Element
- : Tuple_Element<N - 1, T2, T3, VoidType>
- {
- };
- template <size_t N, typename T1, typename T2, typename T3>
- typename Tuple_Element<N, T1, T2, T3>::type& get(Tuple<T1, T2, T3> &tp) {
- typedef typename Tuple_Element<N, T1, T2, T3>::TPType type;
- return ((type &)tp).value;
- }
- #endif
- int main() {
- Tuple<> tmp();
- //cout << typeid(get<0>(tmp)).name() << endl;
- Tuple<int, string, double> tp(3, "2", 1.);
- cout<<get<0>(tp) << " " << typeid(get<0>(tp)).name() <<endl;
- cout<<get<1>(tp) << " " << typeid(get<1>(tp)).name() <<endl;
- cout<<get<2>(tp) << " " << typeid(get<2>(tp)).name() <<endl;
- cout << ((Tuple<string, double> &)tp).value << endl;
- Tuple<int> ft1;
- Tuple<int, float> ft2;
- return 0;
- }
- 详情请访问libgod官网..