std::indirectly_readable_traits
std::indirectly_readable_traits (간접 읽기 가능 타입 특성)
타입 I의 값 타입(연관 value type)을 계산하는 특성 클래스예요. C++20부터 있어요.
출처: cppreference
본문
<iterator> 헤더에 정의돼 있어요.
// (1) 기본 템플릿
template< class I >
struct indirectly_readable_traits {};
// (2) 포인터 특수화
template< class T >
struct indirectly_readable_traits<T*> : /* cond-value-type */<T> {};
// (3) 배열 특수화
template< class I >
requires std::is_array_v<I>
struct indirectly_readable_traits<I>
{ using value_type = std::remove_cv_t<std::remove_extent_t<I>>; };
// (4) const 한정 특수화
template< class T >
struct indirectly_readable_traits<const T> : indirectly_readable_traits<T> {};
// (5) value_type 멤버가 있는 타입
template< /* has-member-value-type */ T >
struct indirectly_readable_traits<T> : /* cond-value-type */<typename T::value_type> {};
// (6) element_type 멤버가 있는 타입
template< /* has-member-element-type */ T >
struct indirectly_readable_traits<T> : /* cond-value-type */<typename T::element_type> {};
// (7) 모호성 방지 특수화
template< /* has-member-value-type */ T >
requires /* has-member-element-type */<T>
struct indirectly_readable_traits<T> {};
타입 I의 값 타입을 계산해요. 프로그램 정의 타입에 대해 특수화할 수 있어요. 여러 규칙이 겹칠 때 모호성(7)을 방지해요.
indirectly_readable_traits<I>::value_type은iter_value_t<I>의 정의에 사용돼요.
예제
#include <iterator>
#include <vector>
#include <type_traits>
int main()
{
static_assert(std::same_as<std::indirectly_readable_traits<int*>::value_type, int>);
static_assert(std::same_as<std::iter_value_t<std::vector<int>::iterator>, int>);
}