從零開始寫STL—模板元編程之tuple

tuple

Class template std::tuple is a fixed-size collection of heterogeneous values. It is a generalization of std::pair.函數

能夠用來在bind中存儲函數指針和參數指針

實現思路

不定參數模板 + 模板偏特化
tuple做爲一個存儲不定參數模板的類,分爲一下幾種偏特化形式:
template<>
struct tuple<> {};
template<typename T, typename... Args>
struct tuple<T, Args...> : public tuple<Args...>
template
struct tuple : public nulltuple
能夠注意到每一個模板struct tuple<T, Args...> : public tuple<Args...>做爲其上一級模板的子類來展開,這樣在獲取tuple_at時從前向後維護一個當前元素的下標計數來存取元素
code

源碼

template<typename... Args> struct tuple;

    template<>
    struct tuple<> {};
    typedef tuple<> nulltuple;

    template<typename T, typename... Args>
    struct tuple<T, Args...> : public tuple<Args...>
    {
        typedef tuple<T, Args...> self_type;
        typedef tuple<Args...> base_type;
        typedef T value_type;
        tuple(const T& v, const Args&... tails) :base_type(tails...), value(v) {}
        tuple(T&& v, Args&&... tails) :base_type(std::move(tails)...), value(std::forward<T>(v)) {}
        tuple(T&& v, Args&... tails) :base_type(std::move(tails)...), value(std::forward<T>(v)) {}
        tuple(T& v, Args&&... tails) :base_type(std::move(tails)...), value(std::forward<T>(v)) {}

        T& getval() 
        {
            return value;
        }

        T value;
    };

    template<typename T>
    struct tuple<T> : public nulltuple
    {
        typedef T value_type;
        tuple(const T& val) :value(val) {}

        T& getval() 
        {
            return value;
        }

        T value;
    };
    template<size_t n, typename T> struct tuple_at;

    template<size_t n, typename T,typename... Args>
    struct tuple_at<n, tuple<T,Args...>>
    {
        typedef typename tuple_at<n - 1, tuple<Args...>>::value_type value_type;
        typedef typename tuple_at<n - 1, tuple<Args...>>::tuple_type tuple_type;
    };

    template<typename T,typename... Args>
    struct tuple_at<0, tuple<T,Args...>>
    {
        typedef T value_type;
        typedef typename tuple<T, Args...> tuple_type;
    };

    template<typename T>
    struct tuple_at<0, tuple<T>>
    {
        typedef T value_type;
        typedef typename tuple<T> tuple_type;
    };

    template<>
    struct tuple_at<0,tuple<>>
    {
        typedef tuple<> tuple_type;
        typedef tuple<> value_type;
    };

    template<size_t n,typename... Args>
    typename tuple_at<n, tuple<Args...>>::value_type& tuple_get(tuple<Args...>& t)
    {
        typedef typename tuple_at<n, tuple<Args...>>::tuple_type ret_tuple_type;
        return static_cast<ret_tuple_type&>(t).getval();    
    }
相關文章
相關標籤/搜索