How can I generate a tuple of N type T's?

守給你的承諾、 提交于 2019-12-10 16:03:07

问题


I want to be able to write generate_tuple_type<int, 3> which would internally have a type alias type which would be std::tuple<int, int, int> in this case.

Some sample usage:

int main()
{
    using gen_tuple_t = generate_tuple_type<int, 3>::type;
    using hand_tuple_t = std::tuple<int, int, int>;
    static_assert( std::is_same<gen_tuple_t, hand_tuple_t>::value, "different types" );
}

How can I accomplish this?


回答1:


Fairly straightforward recursive formulation:

template<typename T, unsigned N, typename... REST>
struct generate_tuple_type
{
 typedef typename generate_tuple_type<T, N-1, T, REST...>::type type;
};

template<typename T, typename... REST>
struct generate_tuple_type<T, 0, REST...>
{
  typedef std::tuple<REST...> type;
};

Live example

[Update]

OK, so I was only thinking about modest values of N. The following formulation is more complex, but also significantly faster and less compiler-crushing for large arguments.

#include <tuple>

template<typename /*LEFT_TUPLE*/, typename /*RIGHT_TUPLE*/>
struct join_tuples
{
};

template<typename... LEFT, typename... RIGHT>
struct join_tuples<std::tuple<LEFT...>, std::tuple<RIGHT...>>
{
  typedef std::tuple<LEFT..., RIGHT...> type;
};

template<typename T, unsigned N>
struct generate_tuple_type
{
  typedef typename generate_tuple_type<T, N/2>::type left;
  typedef typename generate_tuple_type<T, N/2 + N%2>::type right;
  typedef typename join_tuples<left, right>::type type;
};

template<typename T>
struct generate_tuple_type<T, 1>
{
  typedef std::tuple<T> type;
};

template<typename T>
struct generate_tuple_type<T, 0>
{
  typedef std::tuple<> type;
};

int main()
{
  using gen_tuple_t = generate_tuple_type<int, 30000>::type;
  static_assert( std::tuple_size<gen_tuple_t>::value == 30000, "wrong size" );
}

Live example

This version performs at most 2*log(N)+1 template instantiations, assuming your compiler memoizes them. Proof left as an exercise for the reader.




回答2:


You can use std::make_index_sequence to give you a pack long enough, and then just wrap it in the type you need. No recursion necessary:

template <typename T, size_t N>
class generate_tuple_type {
    template <typename = std::make_index_sequence<N>>
    struct impl;

    template <size_t... Is>
    struct impl<std::index_sequence<Is...>> {
        template <size_t >
        using wrap = T;

        using type = std::tuple<wrap<Is>...>;
    };

public:
    using type = typename impl<>::type;
};



回答3:


Check the bottom of this link for an example:

http://en.cppreference.com/w/cpp/utility/integer_sequence.

You'll need to do a little more work to encapsulate the resulting tuple as a type alias, but the crucial construct here is std::integer_sequence and friends.



来源:https://stackoverflow.com/questions/33511753/how-can-i-generate-a-tuple-of-n-type-ts

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!