How to create a new tuple type from an old one and a type in boost?

前端 未结 2 514
傲寒
傲寒 2021-02-08 17:28

I have a tuple type. I want to add a element type in it to get a new tuple type. I can do it like

decltype tuple_cat(MyTuple, std::tuple())
         


        
2条回答
  •  孤街浪徒
    2021-02-08 18:05

    C++14 offers a library to generate a sequence of integers at compile type. This helps manipulating static sequences like tuples and arrays (example). An integer sequence can be obtained

    template
    struct integer_sequence {};
    
    template
    struct implementation : implementation {};
    
    template
    struct implementation<0, Ints...>
    {
        typedef integer_sequence type;
    };
    
    template
    using index_sequence_for = typename implementation::type;
    

    To concat MyTuple and MyType you can write the simple functions:

    template
    auto concat(X x, Tuple t, integer_sequence)
        -> decltype( std::make_tuple(x, std::get(t)...) )
    {
        return std::make_tuple(x, std::get(t)...);
    }
    
    template
    std::tuple concat(X x, std::tuple t)
    {
        return concat(x, t, index_sequence_for());
    }
    
    concat(MyType, MyTuple);
    

提交回复
热议问题