Namespaces
Variants

std:: is_same

From cppreference.net
Metaprogramming library
Type traits
Type categories
(C++11)
(C++11) ( DR* )
Type properties
(C++11)
(C++11)
(C++14)
(C++11) (deprecated in C++26)
(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
Type transformations
(C++11) (deprecated in C++23)
(C++11) (deprecated in C++23)
(C++11)
(C++11) ( until C++20* ) (C++17)

Compile-time rational arithmetic
Compile-time integer sequences
Definiert im Header <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-Qualifizierern), stellt die Member-Konstante value den Wert true bereit. Andernfalls ist value gleich false .

Kommutativität ist erfüllt, d.h. für je zwei Typen T und U , 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.

Inhaltsverzeichnis

Hilfsvariablen-Template

template < class T, class U >
constexpr bool is_same_v = is_same < T, U > :: value ;
(seit C++17)

Geerbt von std:: integral_constant

Member-Konstanten

value
[static]
true wenn T und U denselben Typ haben, false andernfalls
(öffentliche statische Member-Konstante)

Member-Funktionen

operator bool
konvertiert das Objekt zu bool , gibt value zurück
(öffentliche Member-Funktion)
operator()
(C++14)
gibt value zurück
(öffentliche Member-Funktion)

Member-Typen

Typ Definition
value_type bool
type std:: integral_constant < bool , value >

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 {};

Beispiel

#include <cstdint>
#include <iostream>
#include <type_traits>
#define SHOW(...) std::cout << #__VA_ARGS__ << " : " << __VA_ARGS__ << '\n'
int main()
{
    std::cout << std::boolalpha;
    // einige implementierungsabhängige Fakten
    // normalerweise wahr, wenn 'int' 32 Bit ist
    SHOW( std::is_same<int, std::int32_t>::value ); // möglicherweise wahr
    // möglicherweise wahr, wenn ILP64-Datenmodell verwendet wird
    SHOW( std::is_same<int, std::int64_t>::value ); // möglicherweise falsch
    // dieselben Tests wie oben, aber mit C++17s std::is_same_v<T, U>-Format
    SHOW( std::is_same_v<int, std::int32_t> ); // möglicherweise wahr
    SHOW( std::is_same_v<int, std::int64_t> ); // möglicherweise falsch
    // vergleiche die Typen einiger Variablen
    long double num1 = 1.0;
    long double num2 = 2.0;
    static_assert( std::is_same_v<decltype(num1), decltype(num2)> == true );
    // 'float' ist niemals ein integraler Typ
    static_assert( std::is_same<float, std::int32_t>::value == false );
    // 'int' ist implizit '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 );
    // im Gegensatz zu anderen Typen ist 'char' weder 'unsigned' noch '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-qualifizierter Typ T ist nicht derselbe wie nicht-const T
    static_assert( !std::is_same<const int, int>() );
}
#undef SHOW

Mögliche Ausgabe:

std::is_same<int, std::int32_t>::value : true
std::is_same<int, std::int64_t>::value : false
std::is_same_v<int, std::int32_t> : true
std::is_same_v<int, std::int64_t> : false

Siehe auch

(C++20)
spezifiziert, dass ein Typ identisch mit einem anderen Typ ist
(Konzept)
decltype Spezifizierer (C++11) ermittelt den Typ eines Ausdrucks oder einer Entität