Produce std::tuple of same type in compile time given its length by a template argument

前端 未结 5 1901
礼貌的吻别
礼貌的吻别 2021-02-13 10:18

In c++, how can I implement a function with an int template argument indicating the tuple length and produce a std::tuple with that length?

E.g.

func<         


        
5条回答
  •  时光取名叫无心
    2021-02-13 10:44

    If you're okay with a C++14 solution, Ryan's answer is the way to go.

    With C++11, you can do the following (still based on index_sequence, but that's implementable in C++11):

    template >
    struct n_tuple;
    
    template 
    struct n_tuple> {
        template 
        using ignore = T;
    
        using type = std::tuple...>;
    };
    
    template 
    using n_tuple_t = typename n_tuple::type;
    

    With that:

    template 
    n_tuple_t func() {
        return n_tuple_t{};
    }
    

提交回复
热议问题