Difference between revisions of "cpp/utility/tuple/tuple element"
From cppreference.com
(cleanup) |
((hopefully) made example simpler to understand) |
||
Line 41: | Line 41: | ||
#include <tuple> | #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() | 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= | | 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 | ||
}} | }} | ||
Revision as of 01:51, 19 July 2021
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 I th 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
Run this code
#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 |
(C++11) |
obtains the element types of a tuple-like type (class template) |