A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://en.cppreference.com/w/cpp/language/../error/error_code/../../types/type_identity.html below:

std::type_identity - cppreference.com

template< class T >
struct type_identity;

(since C++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.

[edit] Member types [edit] Helper types

template< class T >
using type_identity_t = type_identity<T>::type;

(since C++20) [edit] Possible implementation
template<class T>
struct type_identity { using type = T; };
[edit] Notes

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

[edit] 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:

[edit] See also function object that returns its argument unchanged
(class) [edit]

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4