[ Web Proxy ]
URL:
Viewing: https://de.cppreference.com/cpp/utility/integer_sequence [Back]  [Original]

std::integer_sequence cppreference.com
cppreference.com
Namensrume
Varianten

std::integer_sequence

Aus cppreference.com
<tbody> </tbody>
definiert in Header <utility>
template< class T, T... Ints > class integer_sequence;
(seit C++14)

Das Klassentemplate std::integer_sequence stellt eine Sequenz von Ganzzahlwerten zur Kompilationszeit dar. Wenn es als Argument eines FunktionsTemplates benutzt wird, kann der Parameterpack Ints ermittelt werden und kann im Zusammenhang mit der Entfaltung des Packs benutzt werden.

Templateparameter

T - ein Ganzzahltyp zur Verwendung fr die Elemente der Sequenz
...Ints - ein typenloser Parameterpack, die die Sequenz darstellt

Klassentypen

Klassentyp Definition
value_type T

Methoden

[statisch]
gibt die Anzahl von Elementen in Ints zurck
(public static Elementfunktion)

std::integer_sequence::size

<tbody> </tbody>
static constexpr std::size_t size() noexcept;

Die Method gibt die Anzahl von Elementen in Ints zurck. Dieses ist quivalent zu sizeof...(Ints)

Parameter

(none)

Rckgabewert

Anzahl von Elementen in Ints

Hilfstemplates

Ein Hilfstemplate std::index_sequence ist definiert fr den hufig vorkommenden Fall, bei dem T std::size_t ist:

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

Hilfstemplates std::make_integer_sequence und std::make_index_sequence sind definiert fr die einfache Erzeugung von std::integer_sequence und std::index_sequence mit 0, 1, 2, ..., N-1:

<tbody> </tbody>
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>;

Das Program ist nicht wohlgeformt, falls N negativ ist. Falls N Null ist, ist der erzeugte Type integer_sequence<T>.

Ein Hilfstemplate std::index_sequence_for ist definiert, um jeden typisierte Parameterpacken in eine Indexsequenz der selben Lnge zu berfhren:

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

Anmerkungen

Feature testing macro: __cpp_lib_integer_sequence

Beispiele

Anmerkung: siehe auch mgliche Implementierung in std::apply fr weitere Beispiele

#include <tuple>
#include <iostream>
#include <array>
#include <utility>

// debugging aid
template<typename T, T... ints>
void print_sequence(std::integer_sequence<T, ints...> int_seq)
{
    std::cout << "The sequence of size " << int_seq.size() << ": ";
    ((std::cout << ints << ' '),...);
    std::cout << '\n';
}

// Convert array into a tuple
template<typename Array, std::size_t... I>
auto a2t_impl(const Array& a, std::index_sequence<I...>)
{
    return std::make_tuple(a[I]...);
}

template<typename T, std::size_t N, typename Indices = std::make_index_sequence<N>>
auto a2t(const std::array<T, N>& a)
{
    return a2t_impl(a, Indices{});
}

// pretty-print a tuple

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 == 0? "" : ", ") << std::get<Is>(t)), ...);
}

template<class Ch, class Tr, class... Args>
auto& operator<<(std::basic_ostream<Ch, Tr>& os,
                 const std::tuple<Args...>& t)
{
    os << "(";
    print_tuple_impl(os, t, std::index_sequence_for<Args...>{});
    return os << ")";
}


int main()
{
    print_sequence(std::integer_sequence<unsigned, 9, 2, 5, 1, 9, 1, 6>{});
    print_sequence(std::make_integer_sequence<int, 20>{});
    print_sequence(std::make_index_sequence<10>{});
    print_sequence(std::index_sequence_for<float, std::iostream, char>{});

    std::array<int, 4> array = {1,2,3,4};

    // convert an array into a tuple
    auto tuple = a2t(array);
    static_assert(std::is_same<decltype(tuple),
                               std::tuple<int, int, int, int>>::value, "");

    // print it to cout
    std::cout << tuple << '\n';

}

Output:

The sequence of size 7: 9 2 5 1 9 1 6 
The sequence of size 20: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 
The sequence of size 10: 0 1 2 3 4 5 6 7 8 9 
The sequence of size 3: 0 1 2 
(1, 2, 3, 4)

Web Proxy Viewer  |  New URL  |  Original Page