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

前端 未结 5 1880
礼貌的吻别
礼貌的吻别 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:43

    The plain old recursion is your friend:

    template
    auto array_tuple() {
        return std::tuple_cat(std::tuple{}, array_tuple());
    }
    
    template<>
    auto array_tuple<0>() {
        return std::tuple<>{};
    }
    

提交回复
热议问题