std:: tuple_element <std::tuple>
From cppreference.net
C++
Utilities library
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
std::tuple
| Member functions | ||||
| Non-member functions | ||||
|
(until C++20)
(until C++20)
(until C++20)
(until C++20)
(until C++20)
(C++20)
|
||||
| Helper concepts | ||||
|
(C++23)
|
||||
| Helper classes | ||||
|
tuple_element
<std::tuple>
|
||||
|
(C++23)
|
||||
|
(C++23)
|
||||
| Deduction guides (C++17) |
|
헤더에 정의됨
<tuple>
|
||
|
template
<
std::
size_t
I,
class
...
Types
>
struct tuple_element < I, std:: tuple < Types... > > ; |
(C++11부터) | |
튜플 요소들의 타입에 컴파일 타임 인덱스 접근을 제공합니다.
목차 |
멤버 타입
| 유형 | 정의 |
| type |
튜플의
I
번째
요소의 유형, 여기서
I
는
[
0
,
sizeof...
(
Types
)
)
범위 내에 있음
|
가능한 구현
template<std::size_t I, class T> struct tuple_element; #ifndef __cpp_pack_indexing // 재귀 케이스 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...>> { }; // 기본 케이스 template<class Head, class... Tail> struct tuple_element<0, std::tuple<Head, Tail...>> { using type = Head; }; #else // 팩 인덱싱을 사용한 C++26 구현 template<std::size_t I, class... Ts> struct tuple_element<I, std::tuple<Ts...>> { using type = Ts...[I]; }; #endif |
예제
이 코드 실행
#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>(); }
가능한 출력:
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&&
참고 항목
| Structured binding (C++17) | 지정된 이름들을 초기화자의 부분 객체나 튜플 요소에 바인딩합니다 |
|
(C++11)
|
튜플과 유사한 타입의 요소 타입들을 얻습니다
(클래스 템플릿) |