Namespaces
Variants

std:: type_identity

From cppreference.net
Metaprogramming library
Type traits
Type categories
(C++11)
(C++11) ( DR* )
Type properties
(C++11)
(C++11)
(C++14)
(C++11) (deprecated in C++26)
(C++11) ( until C++20* )
(C++11) (deprecated in C++20)
(C++11)
Type trait constants
Metafunctions
(C++17)
Supported operations
Relationships and property queries
Type modifications
Type transformations
(C++11) (deprecated in C++23)
(C++11) (deprecated in C++23)
(C++11)
(C++11) ( until C++20* ) (C++17)

type_identity
(C++20)
(C++11)
(C++17)
Compile-time rational arithmetic
Compile-time integer sequences
헤더에 정의됨 <type_traits>
template < class T >
struct type_identity ;
(C++20부터)

멤버 typedef type 를 제공하며, 이는 T 를 지칭합니다 (즉, 항등 변환입니다).

프로그램이 std::type_identity 에 대한 특수화를 추가하는 경우, 동작은 정의되지 않습니다.

목차

멤버 타입

이름 정의
type T

헬퍼 타입

template < class T >
using type_identity_t = type_identity < T > :: type ;
(C++20부터)

가능한 구현

template<class T>
struct type_identity { using type = T; };

참고 사항

std::type_identity 는 템플릿 인자 추론에서 비추론 문맥(non-deduced contexts) 을 설정하는 데 사용될 수 있습니다.

기능 테스트 매크로 표준 기능
__cpp_lib_type_identity 201806L (C++20) std::type_identity

예제

#include <iostream>
#include <type_traits>
template<class T>
T foo(T a, T b) { return a + b; }
template<class T>
T bar(T a, std::type_identity_t<T> b) { return a + b; }
int main()
{
    // foo(4.2, 1); // error, deduced conflicting types for 'T'
    std::cout << bar(4.2, 1) << '\n';  // OK, calls bar<double>
}

출력:

5.2

참고 항목

(C++20)
인수를 변경 없이 반환하는 함수 객체
(클래스)