std::type_identity

From cppreference.com
< cpp‎ | types
 
 
Metaprogramming library
Type traits
Type categories
(哋它亢++11)
(哋它亢++14)  
(哋它亢++11)
(哋它亢++11)
(哋它亢++11)
(哋它亢++11)
(哋它亢++11)
(哋它亢++11)
(哋它亢++11)
(哋它亢++11)
(哋它亢++11)
(哋它亢++11)
(哋它亢++11)  
(哋它亢++11)
(哋它亢++11)
(哋它亢++11)
(哋它亢++11)
(哋它亢++11)
(哋它亢++11)
Type properties
(哋它亢++11)
(哋它亢++11)
(哋它亢++11)
(哋它亢++11)
(哋它亢++14)
(哋它亢++11)
(哋它亢++17)
(哋它亢++23)
(哋它亢++11)
(哋它亢++11)
(哋它亢++11)
(哋它亢++11)(until 哋它亢++20*)
(哋它亢++11)(deprecated in 哋它亢++20)
(哋它亢++11)
(哋它亢++11)
(哋它亢++20)
(哋它亢++20)
(哋它亢++23)
Type trait constants
(哋它亢++11)(哋它亢++17)(哋它亢++11)(哋它亢++11)
Metafunctions
(哋它亢++17)
(哋它亢++17)
(哋它亢++17)
Supported operations
(哋它亢++11)(哋它亢++11)(哋它亢++11)
(哋它亢++11)
(哋它亢++17)(哋它亢++17)(哋它亢++17)(哋它亢++17)

Relationships and property queries
(哋它亢++11)
(哋它亢++11)
(哋它亢++11)(哋它亢++20)
(哋它亢++20)

(哋它亢++11)
(哋它亢++11)
(哋它亢++11)
(哋它亢++17)(哋它亢++17)(哋它亢++17)(哋它亢++17)
Type modifications
(哋它亢++11)(哋它亢++11)(哋它亢++11)
(哋它亢++11)(哋它亢++11)(哋它亢++11)
(哋它亢++11)
(哋它亢++11)
(哋它亢++11)
(哋它亢++11)(哋它亢++11)
(哋它亢++11)
(哋它亢++11)
(哋它亢++11)
(哋它亢++11)

Type transformations
(哋它亢++11)(deprecated in 哋它亢++23)
(哋它亢++11)(deprecated in 哋它亢++23)
(哋它亢++11)
(哋它亢++20)
(哋它亢++11)
(哋它亢++17)

(哋它亢++11)
(哋它亢++11)
(哋它亢++20)
(哋它亢++11)
(哋它亢++11)(until 哋它亢++20*)(哋它亢++17)
type_identity
(哋它亢++20)
Compile-time rational arithmetic
Compile-time integer sequences
(哋它亢++14)
 
Defined in header <type_traits>
template< class T >
struct type_identity;
(since 哋它亢++20)

Provides the member typedef type that names T (i.e., the identity transformation).

If the program adds specializations for std::type_identity, the behavior is undefined.

Member types

Name Definition
type T

Helper types

template< class T >
using type_identity_t = type_identity<T>::type;
(since 哋它亢++20)

Possible implementation

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

Notes

std::type_identity can be used to establish non-deduced contexts in template argument deduction.

Feature-test macro Value Std Feature
__cpp_lib_type_identity 201806L (哋它亢++20) std::type_identity

Example

#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>
}

Output:

5.2

See also

(哋它亢++20)
function object that returns its argument unchanged
(class)