Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::integer_sequence

      From cppreference.com
      <cpp‎ |utility
       
       
      Metaprogramming library
      Type traits
      Type categories
      (C++11)
      (C++11)(DR*)
      (C++11)
      (C++11)
      (C++11)
      (C++11)
      (C++11)
      (C++11) 
      Type properties
      (C++11)
      (C++11)
      (C++14)
      (C++11)(deprecated in C++26)
      (C++11)(until C++20*)
      (C++11)(deprecated in C++20)
      (C++11)
      Type trait constants
      Metafunctions
      (C++17)
      Supported operations
      Relationships and property queries
      Type modifications
      Type transformations
      (C++11)(deprecated in C++23)
      (C++11)(deprecated in C++23)
      (C++11)
      (C++11)(until C++20*)(C++17)

      Compile-time rational arithmetic
      Compile-time integer sequences
      integer_sequence
      (C++14)
       
      Defined in header<utility>
      template<class T, T...Ints>
      class integer_sequence;
      (since C++14)

      The class templatestd::integer_sequence represents a compile-time sequence of integers. When used as an argument to afunction template, theparameter packInts can be deduced and used in pack expansion.

      Contents

      [edit]Template parameters

      T - an integer type to use for the elements of the sequence
      ...Ints - a constant parameter pack representing the sequence

      [edit]Member types

      Type Definition
      value_typeT

      [edit]Member functions

      size
      [static]
      returns the number of elements inInts
      (public static member function)

      std::integer_sequence::size

      staticconstexprstd::size_t size()noexcept;

      Returns the number of elements inInts. Equivalent tosizeof...(Ints).

      Return value

      The number of elements inInts.

      [edit]Helper templates

      A helper alias templatestd::index_sequence is defined for the common case whereT isstd::size_t:

      template<std::size_t...Ints>
      using index_sequence= std::integer_sequence<std::size_t, Ints...>;

      Helper alias templatesstd::make_integer_sequence andstd::make_index_sequence are defined to simplify creation ofstd::integer_sequence andstd::index_sequence types, respectively, with0,1,2,...,N-1 asInts:

      template<class T, T N>
      using make_integer_sequence= std::integer_sequence<T,/* a sequence 0, 1, 2, ..., N-1 */>;
      template<std::size_t N>
      using make_index_sequence= std::make_integer_sequence<std::size_t, N>;

      The program is ill-formed ifN is negative. IfN is zero, the indicated type isinteger_sequence<T>.

      A helper alias templatestd::index_sequence_for is defined to convert any type parameter pack into an index sequence of the same length:

      template<class...T>
      using index_sequence_for= std::make_index_sequence<sizeof...(T)>;

      [edit]Notes

      Feature-test macroValueStdFeature
      __cpp_lib_integer_sequence201304L(C++14)Compile-time integer sequences

      [edit]Possible implementation

      make_integer_sequence
      namespace detail{template<class T, T I, T N, T...integers>struct make_integer_sequence_helper{using type=typename make_integer_sequence_helper<T, I+1, N, integers..., I>::type;}; template<class T, T N, T...integers>struct make_integer_sequence_helper<T, N, N, integers...>{using type= std::integer_sequence<T, integers...>;};} template<class T, T N>using make_integer_sequence= detail::make_integer_sequence_helper<T,0, N>::type;

      [edit]Example

      See alsostd::apply possible implementation for another example.

      Run this code
      #include <array>#include <cstddef>#include <iostream>#include <tuple>#include <utility> namespace details{template<typename Array,std::size_t...I>constexprauto array_to_tuple_impl(const Array& a, std::index_sequence<I...>){returnstd::make_tuple(a[I]...);} template<class Ch,class Tr,class Tuple,std::size_t...Is>void print_tuple_impl(std::basic_ostream<Ch, Tr>& os,const Tuple& t,                      std::index_sequence<Is...>){((os<<(Is?", ":"")<< std::get<Is>(t)), ...);}} template<typename T, T...ints>void print_sequence(int id, std::integer_sequence<T, ints...> int_seq){std::cout<< id<<") The sequence of size "<< int_seq.size()<<": ";((std::cout<< ints<<' '), ...);std::cout<<'\n';} template<typename T,std::size_t N,typename Indx= std::make_index_sequence<N>>constexprauto array_to_tuple(conststd::array<T, N>& a){return details::array_to_tuple_impl(a, Indx{});} template<class Ch,class Tr,class...Args>auto& operator<<(std::basic_ostream<Ch, Tr>& os,conststd::tuple<Args...>& t){    os<<'(';    details::print_tuple_impl(os, t, std::index_sequence_for<Args...>{});return os<<')';} int main(){    print_sequence(1, std::integer_sequence<unsigned,9,2,5,1,9,1,6>{});    print_sequence(2, std::make_integer_sequence<int,12>{});    print_sequence(3, std::make_index_sequence<10>{});    print_sequence(4, std::index_sequence_for<std::ios,float,signed>{}); constexprstd::array<int,4> array{1,2,3,4}; auto tuple1= array_to_tuple(array);    static_assert(std::is_same_v<decltype(tuple1),std::tuple<int,int,int,int>>,"");std::cout<<"5) tuple1: "<< tuple1<<'\n'; constexprauto tuple2= array_to_tuple<int,4,        std::integer_sequence<std::size_t,1,0,3,2>>(array);std::cout<<"6) tuple2: "<< tuple2<<'\n';}

      Output:

      1) The sequence of size 7: 9 2 5 1 9 1 6 2) The sequence of size 12: 0 1 2 3 4 5 6 7 8 9 10 11 3) The sequence of size 10: 0 1 2 3 4 5 6 7 8 9 4) The sequence of size 3: 0 1 2 5) tuple1: (1, 2, 3, 4)6) tuple2: (2, 1, 4, 3)

      [edit]See also

      (C++20)
      creates astd::array object from a built-in array
      (function template)[edit]
      compile-time constant of specified type with specified value
      (class template)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/utility/integer_sequence&oldid=182736"

      [8]ページ先頭

      ©2009-2025 Movatter.jp