tuple_tuple_element
tuple_tuple_element (튜플 요소 타입 접근)
이 페이지에서는 std::tuple_element에 대해 설명해요. std::tuple_element는 컴파일 타임에 튜플의 특정 인덱스에 해당하는 요소 타입을 조회할 수 있게 해주는 클래스 템플릿이에요. C++11부터 사용할 수 있어요.
출처: cppreference
본문
정의
헤더 <tuple>에 정의됨 |
||
|---|---|---|
template < std :: size_t I , class ... Types > struct tuple_element < I , std :: tuple < Types ... > > ; |
(C++11부터) |
tuple의 요소 타입들에 대해 컴파일 타임 인덱스 접근을 제공해요.
멤버 타입
| 타입 | 설명 |
|---|---|
type |
튜플의 I번째 요소의 타입이에요. 여기서 I는 [0, sizeof...(Types)) 범위에 있어요. |
가능한 구현
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
예제
#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) | 초기화자의 하위 객체나 튜플 요소에 지정된 이름을 바인딩해요. |
tuple_element (C++11) |
튜플 유사 타입의 요소 타입을 얻어요. (클래스 템플릿) |
tuple_element (C++26) |
튜플 유사 타입의 요소 타입에 대한 리플렉션을 얻어요. (함수) |
std::tuple_element <std::pair> (C++11) |
pair의 요소 타입을 얻어요. (클래스 템플릿 특수화) |
std::tuple_element <std::array> (C++11) |
array의 요소 타입을 얻어요. (클래스 템플릿 특수화) |
std::tuple_element <std::ranges::subrange> (C++20) |
std::ranges::subrange의 반복자 또는 센티널 타입을 얻어요. (클래스 템플릿 특수화) |
std::tuple_element <std::complex> (C++26) |
std::complex의 기반이 되는 실수부와 허수부 타입을 얻어요. (클래스 템플릿 특수화) |
std::tuple_size <std::tuple> (C++11) |
튜플의 크기를 얻어요. (클래스 템플릿 특수화) |
get (std::tuple) (C++11) |
튜플에서 지정된 요소에 접근해요. (함수 템플릿) |