Sequence array initialization with template

前端 未结 1 1040
有刺的猬
有刺的猬 2020-12-19 12:26

I want to initialize an array with a sequence of ints from 0 to N - 1

#include 
#include          


        
1条回答
  •  無奈伤痛
    2020-12-19 13:04

    I'm not sure if this meets your requirements.

    #include 
    #include 
    
    template 
    constexpr auto init(std::index_sequence) {
        return std::array{I...};
    }
    
    int main(void)
    {
        std::array a = init(std::make_index_sequence<10>());
    
        for (int const & i : a)
            std::cout << i << "\n";
        return 0;
    }
    

    0 讨论(0)
提交回复
热议问题