std::type_identity

Header: <type_traits>

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

# Declarations

template< class T >
struct type_identity;

(since C++20)

# Notes

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

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

# See also