How can I use C++11 variadic templates to define a vector-of-tuples backed by a tuple-of-vectors?

后端 未结 6 655
夕颜
夕颜 2020-12-13 11:20

Suppose I have a bunch of vectors:

vector v1;
vector v2;
vector v3;

all of the same length. Now, for ev

6条回答
  •  时光说笑
    2020-12-13 11:47

    Conversion to a std::tuple of vectors (vector::iterators):

    #include 
    #include 
    
    // identity
    // ========
    
    struct identity
    {
        template 
        struct apply {
            typedef T type;
        };
    };
    
    // concat_operation
    // ================
    
    template  struct concat_operation;
    
    template <
        typename Operator,
        typename ...Types,
        typename T>
    struct concat_operation, T>
    {
        private:
        typedef typename Operator::template apply::type concat_type;
        public:
        typedef std::tuple type;
    };
    
    template <
        typename Operator,
        typename ...Types,
        typename T,
        typename ...U>
    struct concat_operation, T, U...>
    {
        private:
        typedef typename Operator::template apply::type concat_type;
        public:
        typedef typename concat_operation<
            Operator,
            std::tuple,
            U...>
        ::type type;
    };
    
    template <
        typename Operator,
        typename T,
        typename ...U>
    struct concat_operation
    {
        private:
        typedef typename Operator::template apply::type concat_type;
        public:
        typedef typename concat_operation<
            Operator,
            std::tuple,
            U...>
        ::type type;
    };
    
    
    // ToVectors (ToVBackedVoT)
    // =========
    
    template 
    struct ToVectors
    {
        private:
        struct to_vector {
            template 
            struct apply {
                typedef typename std::vector type;
            };
        };
    
        public:
        typedef typename concat_operation::type type;
    };
    
    // ToIterators
    // ===========
    
    template 
    struct ToIterators;
    
    template 
    struct ToIterators>
    {
        private:
        struct to_iterator {
            template 
            struct apply {
                typedef typename V::iterator type;
            };
        };
    
        public:
        typedef typename concat_operation::type type;
    };
    
    
    int main() {
        typedef ToVectors::type Vectors;
        typedef ToVectors::type MoreVectors;
        typedef ToIterators::type Iterators;
    
        // LOG_TYPE(Vectors);
        // std::tuple<
        //     std::vector >,
        //     std::vector >,
        //     std::vector > >
    
        // LOG_TYPE(Iterators);
        // std::tuple<
        //     __gnu_cxx::__normal_iterator > >,
        //     __gnu_cxx::__normal_iterator > >,
        //     __gnu_cxx::__normal_iterator > > >
    }
    

提交回复
热议问题