std::is_same
Von cppreference.com
| Definiert in der Kopfdatei <type_traits> |
||
| template< class T, class U > struct is_same; |
(seit C++11) | |
Wenn T und U denselben Typ bezeichnen (unter Berücksichtigung von const/volatile-Qualifizierungen), liefert dies den Member-Konstanten value, der gleich true ist. Andernfalls ist value false.
Kommutativität ist erfüllt, d.h. für beliebige zwei Typen T und U gilt is_same<T, U>::value == true genau dann, wenn is_same<U, T>::value == true.
Wenn das Programm Spezialisierungen für std::is_same oder std::is_same_v(seit C++17) hinzufügt, ist das Verhalten undefiniert.
Inhalt |
[edit] Hilfsvariablentemplate
| template< class T, class U > constexpr bool is_same_v = is_same<T, U>::value; |
(seit C++17) | |
Abgeleitet von std::integral_constant
Member-Konstanten
| value [static] |
true, wenn T und U derselbe Typ sind, ansonsten false(öffentliche statische Member-Konstante) |
Memberfunktionen
| operator bool |
konvertiert das Objekt zu bool, gibt value zurück (öffentliche Memberfunktion) |
| operator() (C++14) |
gibt value zurück (öffentliche Memberfunktion) |
Membertypen
| Typ | Definition |
value_type
|
bool |
type
|
std::integral_constant<bool, value> |
[edit] Mögliche Implementierung
template<class T, class U> struct is_same : std::false_type {}; template<class T> struct is_same<T, T> : std::true_type {}; |
[edit] Beispiel
Führen Sie diesen Code aus
#include <cstdint> #include <iostream> #include <type_traits> int main() { std::cout << std::boolalpha; // some implementation-defined facts // usually true if 'int' is 32 bit std::cout << std::is_same<int, std::int32_t>::value << ' '; // maybe true // possibly true if ILP64 data model is used std::cout << std::is_same<int, std::int64_t>::value << ' '; // maybe false // same tests as above, except using C++17's std::is_same_v<T, U> format std::cout << std::is_same_v<int, std::int32_t> << ' '; // maybe true std::cout << std::is_same_v<int, std::int64_t> << '\n'; // maybe false // compare the types of a couple variables long double num1 = 1.0; long double num2 = 2.0; static_assert( std::is_same_v<decltype(num1), decltype(num2)> == true ); // 'float' is never an integral type static_assert( std::is_same<float, std::int32_t>::value == false ); // 'int' is implicitly 'signed' static_assert( std::is_same_v<int, int> == true ); static_assert( std::is_same_v<int, unsigned int> == false ); static_assert( std::is_same_v<int, signed int> == true ); // unlike other types, 'char' is neither 'unsigned' nor 'signed' static_assert( std::is_same_v<char, char> == true ); static_assert( std::is_same_v<char, unsigned char> == false ); static_assert( std::is_same_v<char, signed char> == false ); // const-qualified type T is not same as non-const T static_assert( !std::is_same<const int, int>() ); }
Mögliche Ausgabe
true false true false
[edit] Siehe auch
| (C++20) |
gibt an, dass ein Typ identisch mit einem anderen Typ ist (Konzept) |
decltype-Spezifizierer(C++11) |
erhält den Typ eines Ausdrucks oder einer Entität |