Namensräume
Varianten
Aktionen

std::tuple_element<std::tuple>

Von cppreference.com
< cpp‎ | utility‎ | tuple
 
 
Dienstprogramm-Bibliotheken
Sprachunterstützung
Typunterstützung (Basistypen, RTTI)
Bibliotheks-Feature-Test-Makros (C++20)
Programm-Dienstprogramme
Variadische Funktionen
Coroutine-Unterstützung (C++20)
Vertragsunterstützung (C++26)
Drei-Wege-Vergleich
(C++20)
(C++20)(C++20)(C++20)  
(C++20)(C++20)(C++20)

Allgemeine Hilfsmittel
Relationale Operatoren (in C++20 veraltet)
 
 
Definiert in der Header-Datei <tuple>
template< std::size_t I, class... Types >
struct tuple_element< I, std::tuple<Types...> >;
(seit C++11)

Bietet zur Kompilierzeit indizierten Zugriff auf die Typen der Elemente des Tupels.

Inhalt

[bearbeiten] Member types

Typ Definition
type der Typ des I-ten Elements des Tupels, wobei I im Bereich [0sizeof...(Types)) liegt

[bearbeiten] Mögliche Implementierung

template<std::size_t I, class T>
struct tuple_element;
 
#ifndef __cpp_pack_indexing
// recursive case
template<std::size_t I, class Head, class... Tail>
struct tuple_element<I, std::tuple<Head, Tail...>>
    : std::tuple_element<I - 1, std::tuple<Tail...>>
{ };
 
// base case
template<class Head, class... Tail>
struct tuple_element<0, std::tuple<Head, Tail...>>
{
    using type = Head;
};
 
#else
// C++26 implementation using pack indexing
template<std::size_t I, class... Ts>
struct tuple_element<I, std::tuple<Ts...>>
{
    using type = Ts...[I];
};
#endif

[bearbeiten] Beispiel

#include <boost/type_index.hpp>
#include <cstddef>
#include <iostream>
#include <string>
#include <tuple>
#include <utility>
 
template<typename TupleLike, std::size_t I = 0>
void printTypes()
{
    if constexpr (I == 0)
        std::cout << boost::typeindex::type_id_with_cvr<TupleLike>() << '\n';
 
    if constexpr (I < std::tuple_size_v<TupleLike>)
    {
        using SelectedType = std::tuple_element_t<I, TupleLike>;
 
        std::cout << "  The type at index " << I << " is: "
                  << boost::typeindex::type_id_with_cvr<SelectedType>() << '\n';
        printTypes<TupleLike, I + 1>();
    }
}
 
struct MyStruct {};
 
using MyTuple = std::tuple<int, long&, const char&, bool&&,
                           std::string, volatile MyStruct>;
 
using MyPair = std::pair<char, bool&&>;
 
static_assert(std::is_same_v<std::tuple_element_t<0, MyPair>, char>);
static_assert(std::is_same_v<std::tuple_element_t<1, MyPair>, bool&&>);
 
int main()
{
    printTypes<MyTuple>();
    printTypes<MyPair>();
}

Mögliche Ausgabe

std::tuple<int, long&, char const&, bool&&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, MyStruct volatile>
  The type at index 0 is: int
  The type at index 1 is: long&
  The type at index 2 is: char const&
  The type at index 3 is: bool&&
  The type at index 4 is: std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >
  The type at index 5 is: MyStruct volatile
std::pair<char, bool&&>
  The type at index 0 is: char
  The type at index 1 is: bool&&

[bearbeiten] Siehe auch

Strukturierte Bindung (C++17) bindet die angegebenen Namen an Unterobjekte oder Tuple-Elemente des Initialisierers[bearbeiten]
erhält die Elementtypen eines Tupel-ähnlichen Typs
(Klassenvorlage) [edit]