Implementation C++14 make_integer_sequence

前端 未结 7 689
-上瘾入骨i
-上瘾入骨i 2020-11-22 03:28

I tried to implement the C++14 alias template make_integer_sequence, which simplifies the creation of the class template integer_sequence.

templ         


        
7条回答
  •  灰色年华
    2020-11-22 03:57

    Here's a log N implementation that doesn't even need an increased max-depth for template instantiations and compiles pretty fast:

    // using aliases for cleaner syntax
    template using Invoke = typename T::type;
    
    template struct seq{ using type = seq; };
    
    template struct concat;
    
    template
    struct concat, seq>
      : seq{};
    
    template
    using Concat = Invoke>;
    
    template struct gen_seq;
    template using GenSeq = Invoke>;
    
    template
    struct gen_seq : Concat, GenSeq>{};
    
    template<> struct gen_seq<0> : seq<>{};
    template<> struct gen_seq<1> : seq<0>{};
    

提交回复
热议问题