std::is_same
Определён в заголовочном файле <type_traits> | ||
|---|---|---|
template< class T, class U > struct is_same; | (начиная с C++11) |
Если T и U обозначают один и тот же тип (с учётом квалификаторов const/volatile), предоставляет константу-член value, равную true. В противном случае value равно false.
Выполняется коммутативность, то есть для любых двух типов T и U is_same<T, U>::value == true тогда и только тогда, когда is_same<U, T>::value == true.
Если программа добавляет специализации для std::is_same или std::is_same_v(начиная с C++17), поведение не определено.
Вспомогательный шаблон переменной
template< class T, class U > constexpr bool is_same_v = is_same<T, U>::value; | (начиная с C++17) |
Унаследовано от std::integral_constant
Константы-члены
|
value
[static] |
true, если T и U являются одним и тем же типом, иначе false (открытая статическая константа-член) |
Функции-члены
|
operator bool | преобразует объект в bool, возвращает value (открытая функция-член) |
|
operator()
(C++14) | возвращает value (открытая функция-член) |
Типы-члены
| Тип | Определение |
|---|---|
value_type | bool |
type | std::integral_constant<bool,value> |
Возможная реализация
template<class T, class U>
struct is_same : std::false_type {};
template<class T>
struct is_same<T, T> : std::true_type {};
|
Пример
#include <cstdint>
#include <iostream>
#include <type_traits>
#define SHOW(...) std::cout << #__VA_ARGS__ << " : " << __VA_ARGS__ << '\n'
int main()
{
std::cout << std::boolalpha;
// some implementation-defined facts
// usually true if 'int' is 32 bit
SHOW( std::is_same<int, std::int32_t>::value ); // maybe true
// possibly true if ILP64 data model is used
SHOW( 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
SHOW( std::is_same_v<int, std::int32_t> ); // maybe true
SHOW( std::is_same_v<int, std::int64_t> ); // 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>() );
}
#undef SHOW
Возможный вывод:
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
См. также
decltype спецификатор(C++11) | получает тип выражения или сущности |
|
(C++20) | задаёт требование, что один тип совпадает с другим типом (концепт) |
|
(C++26) | проверяет, представляют ли два отражения один и тот же тип (функция) |
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/cpp/types/is_same