zoukankan      html  css  js  c++  java
  • c++11 tuple实现

    实现一个简易版的c++11 tuple。

    我使用的编译器是gcc,codeblocks13.12自带的,哪个版本我不熟gcc也没去查。

    大致看了下他家的tuple实现,多继承,tuple之上还有2个辅助类,走的是类似loki中GenScatterHierarchy的路子。1092行代码,不是盖的。。。

    有些强迫症,不打算用多继承,,虽然并不会实例化来,看着闹心。

    只考虑实现到POD类型的基本支持就行了,什么右值之类的我还没看到,就不搞了,仅供参考。

    个人觉得tuple保存POD类型值就足够了,泛滥就堕落了。

     单继承版本确实比多继承版本美得多了,可变模板参数真是个好东西。

    为了tuple_get只需使用static_cast,tuple是public继承的。当然私有继承更好,只是。。。我想tuple的使用应该没有地方会造成歧义吧。

    提供了一个tuuple_get<int>(const tuple&)接口获取指定位置的值。

    #ifndef HI_MPL_TUPLE_H_INCLUDE
    #define HI_MPL_TUPLE_H_INCLUDE
    
    namespace hi {
        using namespace mpl::utils;
      //////////////////////////////////////////////////////////
        template<typename... TList> struct tuple;
    
        template<> struct tuple<> {};
    
        typedef tuple<> nulltuple;
    
        //////////////////////////////////////////////////////////
        template<typename T, typename... TList>
        struct tuple<T, TList...> : public tuple<TList...>
        {
            typedef T value_type;
            typedef tuple<TList...> base_type;
            typedef tuple<T, TList...> this_type;
    
            tuple(const T& v, const TList&... tails):base_type(tails...),_value(v) {}
    
            tuple(T&& v, TList&&... tails):base_type(std::move(tails)...), _value(std::forward<T>(v)) {}
            tuple(T&& v, TList&... tails):base_type(std::move(tails)...), _value(std::forward<T>(v)) {}
            tuple(T& v, TList&&... tails):base_type(std::move(tails)...), _value(std::forward<T>(v)) {}
    
            tuple(const this_type& other):base_type(static_cast<const base_type&>(other)),_value(other._value)
            {}
    
            tuple(this_type&& other):base_type(std::move(static_cast<base_type&>(other))),_value(std::forward<T>(other._value))
            {}
    
            const T& head() const { return this->_value; }
            T& head() { return this->_value; }
    
            this_type& operator=(const this_type& other)
            {
                base_type::operator=(static_cast<const base_type&>(other));
                _value = other._value;
                return *this;
            }
    
            this_type& operator=(this_type&& other)
            {
                base_type::operator=(std::move(static_cast<base_type&>(other)));
                _value = other._value;
                return *this;
            }
    
    
        protected:
            T _value;
        };
    
        template<typename T>
        struct tuple<T> : public nulltuple
        {
            typedef T value_type;
            typedef nulltuple base_type;
            typedef tuple<T> this_type;
    
            tuple(const T& v):_value(v) {}
            tuple(T&& v):_value(std::forward<T>(v)) {}
    
            tuple(const this_type& other):_value(other._value) {}
    
            tuple(this_type&& other):_value(std::forward<T>(other._value)) {}
    
            const T& head() const { return this->_value; }
            T& head() { return this->_value; }
            this_type& operator=(const this_type& other)
            {
                _value = other._value;
                return *this;
            }
            this_type& operator=(this_type&& other)
            {
                _value = other._value;
                return *this;
            }
    
        protected:
            T _value;
        };
    
    
        //////////////////////////////////////////////////////////
        template<unsigned int N, typename... TList> struct tuple_at;
    
        template<unsigned int N, typename T, typename... TList>
        struct tuple_at< N, tuple<T, TList...> >
        {
            typedef typename tuple_at< N-1, tuple<TList...> >::value_type value_type;
            typedef typename tuple_at< N-1, tuple<TList...> >::tuple_type tuple_type;
        };
    
        template<typename T, typename... TList>
        struct tuple_at< 0, tuple<T, TList...> >
        {
            typedef T value_type;
            typedef tuple<T, TList...> tuple_type;
        };
    
        template<>
        struct tuple_at<0, nulltuple>
        {
            typedef nulltuple value_type;
            typedef nulltuple tuple_type;
        };
    
        //////////////////////////////////////////////////////////
        template<unsigned int N, typename... TList>
        constexpr const typename tuple_at<N, tuple<TList...> >::value_type&
        tuple_get(const tuple<TList...>& tuple_)
        {
            typedef tuple<TList...> tuple_type;
            typedef typename tuple_at<N, tuple_type>::tuple_type base_tuple_type;
    
            return static_cast<const base_tuple_type&>(tuple_).head();
        }
    
        template<unsigned int N, typename... TList>
        typename tuple_at<N, tuple<TList...> >::value_type&
        tuple_get(tuple<TList...>& tuple_)
        {
            typedef tuple<TList...> tuple_type;
            typedef typename tuple_at<N, tuple_type>::tuple_type base_tuple_type;
    
            return static_cast<base_tuple_type&>(tuple_).head();
        }
    }
    #endif

    例子:

    #include "TypeTuple.h"
    #include <tuple>
    
    int main()
    {
        bool b;
        tuple<int, float, char> pp = {10, 0.1234, 'a'};
        b = std::is_same<tuple_at<2, tuple<int, float, char>>::value_type, char >::value;
        std::cout << "is same: " << b << std::endl;
        b = std::is_same<tuple_at<2, tuple<int, float, char>>::tuple_type, tuple<char> >::value;
        std::cout << "is same: " << b << std::endl;
        std::cout << tuple_get<0>(pp)<<" "<< tuple_get<1>(pp) <<" "<<tuple_get<2>(pp) << std::endl;
        std::tuple<int, float, char> cc{10, 0.1234, 'a'};
        std::cout << sizeof(pp) << "  " << sizeof(cc) << std::endl;
        tuple<int, float, char> ppc = pp;
        std::cout << tuple_get<0>(ppc)<<" "<< tuple_get<1>(ppc) <<" "<<tuple_get<2>(ppc) << std::endl;
        return 0;
    }
  • 相关阅读:
    MySQL-基本sql命令
    Java for LeetCode 203 Remove Linked List Elements
    Java for LeetCode 202 Happy Number
    Java for LeetCode 201 Bitwise AND of Numbers Range
    Java for LeetCode 200 Number of Islands
    Java for LeetCode 199 Binary Tree Right Side View
    Java for LeetCode 198 House Robber
    Java for LeetCode 191 Number of 1 Bits
    Java for LeetCode 190 Reverse Bits
    Java for LeetCode 189 Rotate Array
  • 原文地址:https://www.cnblogs.com/flytrace/p/3574647.html
Copyright © 2011-2022 走看看