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

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

    Here is a recursive solution with alias template and it's implementable in C++11:

    template  
    struct tuple_n{
        template< typename...Args> using type = typename tuple_n::template type;
    };
    
    template  
    struct tuple_n<0, T> {
        template using type = std::tuple;   
    };
    template   using tuple_of = typename tuple_n::template type<>;
    

    For example if we want "tuple of 3 doubles" we can write:

    tuple_of<3, double> t;
    

提交回复
热议问题