Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/utility/tuple/tuple element"

From cppreference.com
< cpp‎ | utility‎ | tuple
(cleanup)
((hopefully) made example simpler to understand)
Line 41: Line 41:
 
#include <tuple>
 
#include <tuple>
  
template <class... Args>
+
void printHelper(int) { std::cout << "int\n"; }
struct type_list
+
void printHelper(bool) { std::cout << "bool\n"; }
 +
void printHelper(char) { std::cout << "char\n"; }
 +
void printHelper(std::string) { std::cout << "std::string\n"; }
 +
void printHelper(...) { std::cout << "unknown type\n"; }
 +
 
 +
template<std::size_t I, class T> void pintTypeAtIndex()
 
{
 
{
  template <std::size_t N>
+
    std::cout << "index " << I << " has type: ";
  using type = typename std::tuple_element<N, std::tuple<Args...>>::type;
+
    using SelectedType = typename std::tuple_element<I, T>::type;
};
+
    // create default initialized instance of that type
 +
    printHelper(SelectedType());  
 +
}
  
 
int main()
 
int main()
 
{
 
{
  std::cout << std::boolalpha;
+
    struct MyStruct {};
  type_list<int, char, bool>::type<2> x = true;
+
    using MyTuple = std::tuple<int, char, bool, std::string, MyStruct>;
  std::cout << x << '\n';
+
    pintTypeAtIndex<0, MyTuple>();
 +
    pintTypeAtIndex<1, MyTuple>();
 +
    pintTypeAtIndex<2, MyTuple>();
 +
    pintTypeAtIndex<3, MyTuple>();
 +
    pintTypeAtIndex<4, MyTuple>();
 
}
 
}
 
  | output=
 
  | output=
true
+
index 0 has type: int
 +
index 1 has type: char
 +
index 2 has type: bool
 +
index 3 has type: std::string
 +
index 4 has type: unknown type
 
}}
 
}}
  

Revision as of 01:51, 19 July 2021

 
 
Utilities library
General utilities
Relational operators (deprecated in C++20)
 
 
Defined in header <tuple>
template< std::size_t I, class... Types >
struct tuple_element< I, std::tuple<Types...> >;
(since C++11)

Provides compile-time indexed access to the types of the elements of the tuple.

Contents

Member types

Member type Definition
type the type of Ith element of the tuple, where I is in [0, sizeof...(Types))

Possible implementation

template< std::size_t I, class T >
struct tuple_element;
 
// 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;
};

Example

#include <iostream>
#include <tuple>
 
void printHelper(int) { std::cout << "int\n"; }
void printHelper(bool) { std::cout << "bool\n"; }
void printHelper(char) { std::cout << "char\n"; }
void printHelper(std::string) { std::cout << "std::string\n"; }
void printHelper(...) { std::cout << "unknown type\n"; }
 
template<std::size_t I, class T> void pintTypeAtIndex()
{
    std::cout << "index " << I << " has type: ";
    using SelectedType = typename std::tuple_element<I, T>::type;
    // create default initialized instance of that type
    printHelper(SelectedType()); 
}
 
int main()
{
    struct MyStruct {};
    using MyTuple = std::tuple<int, char, bool, std::string, MyStruct>;
    pintTypeAtIndex<0, MyTuple>();
    pintTypeAtIndex<1, MyTuple>();
    pintTypeAtIndex<2, MyTuple>();
    pintTypeAtIndex<3, MyTuple>();
    pintTypeAtIndex<4, MyTuple>();
}

Output:

index 0 has type: int
index 1 has type: char
index 2 has type: bool
index 3 has type: std::string
index 4 has type: unknown type

See also

Structured binding (C++17) binds the specified names to sub-objects or tuple elements of the initializer[edit]
obtains the element types of a tuple-like type
(class template) [edit]