std::type_identity

From cppreference.com
< cpp‎ | types
 
 
Utilities library
General utilities
Date and time
Function objects
Formatting library (C++20)
(C++11)
Relational operators (deprecated in C++20)
Integer comparison functions
(C++20)(C++20)(C++20)   
(C++20)
Swap and type operations
(C++14)
(C++11)

(C++11)
(C++11)
(C++17)
Common vocabulary types
(C++11)
(C++17)
(C++17)
(C++17)
(C++11)
(C++17)

Elementary string conversions
(C++17)
(C++17)
 
Type support
Basic types
Fixed width integer types (C++11)
Numeric limits
C numeric limits interface
Runtime type information
Type traits
Type categories
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
Type properties
(C++11)
(C++11)
(C++14)
(C++11)

(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
(C++11)(C++11)(C++11)
Type transformations
(C++11)
(C++11)
(C++17)

(C++11)(until C++20)(C++17)
type_identity
(C++20)
 
Defined in header <type_traits>
template< class T >
struct type_identity;
(since C++20)

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

The behavior of a program that adds specializations for type_identity is undefined.

Member types

Name Definition
type T

Helper types

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

Possible implementation

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

Notes

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

Feature testing macro: __cpp_lib_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