Implementation C++14 make_integer_sequence

前端 未结 7 798
-上瘾入骨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 04:14

    Here is another implementation technique (for T=size_t), it uses C++17 fold expressions and bitwise generation (i.e. O(log(N)):

    template 
    struct idx_seq {
      template 
      struct pow2_impl {
        using type = typename idx_seq::template pow2_impl::type;
      };
      template  struct pow2_impl<0, _> { using type = idx_seq; };
      template  struct pow2_impl<(size_t)-1, _> { using type = idx_seq<>; };
    
      template 
      using offset = idx_seq<(Offset + Is)...>;
    };
    
    template 
    using idx_seq_pow2 = typename idx_seq<0>::template pow2_impl::type;
    
    template 
    constexpr static auto operator,(idx_seq, idx_seq)
      -> idx_seq
    { return {}; }
    
    template 
    struct make_idx_seq_impl {
      using type = typename make_idx_seq_impl= Mask ? Mask << 1 : 0), Bits..., (N & Mask)>::type;
    };
    
    template 
    struct make_idx_seq_impl {
      using type = decltype((idx_seq<>{}, ..., typename idx_seq_pow2::template offset<(N & (Bits - 1))>{}));
    };
    
    template 
    using make_idx_seq = typename make_idx_seq_impl::type;
    

提交回复
热议问题