C++ 语言
std::is_same
std::is_same
定义于头文件 <type_traits>
1. std::is_same
template <class T, class U> struct is_same;
判断两个类型是否相同。
Trait class that identifies whether T
is the same type as U
, including having the same const
and/or volatile
qualification, if any.
Trait class 识别 T
是否与 U
类型相同,包括是否具有相同的 const
和/或 volatile
限定。
若 T
与 U
为同一类型 (考虑 const/volatile 限定),则提供等于 true 的成员常量 value 。否则 value 为 false 。满足交换律,即对于任何二个类型 T
与 U
, is_same<T, U>::value == true
当且仅当 is_same<U, T>::value == true
。
Two different type names are considered to represent the same type if -and only if- one is a typedef
of the other: Two names representing types with the exact same characteristics but which none is a typedef of the other are not considered the same type.
当且仅当一个是另一个的 typedef
时,两个不同的类型名称被认为表示相同的类型:表示具有完全相同特征,但一个不是另一个的 typedef
类型的两个名称不被视为相同的类型。
is_same
inherits from integral_constant
as being either true_type
or false_type
, depending on whether T
and U
are the same type.
is_same
从 integral_constant
继承 true_type
或 false_type
,取决于 T
和 U
是否是相同的类型。
继承自 std::integral_constant
。
2. Template parameters
T
, U
: Types.
3. Member types - 成员类型
Inherited from integral_constant
:
member type | definition |
---|---|
value_type | bool |
type | either true_type or false_type |
4. Member constants - 成员常量
Inherited from integral_constant
:
member constant | definition |
---|---|
value | either true or false |
若 T
与 U
是同一类型则为 true ,否则为 false (公开静态成员常量)。
5. Member functions
Inherited from integral_constant
:
operator bool - Returns value (public member function )
转换对象为 bool ,返回 value (公开成员函数)
inherit [ɪnˈherɪt]:v. 继承 (金钱、财产等),经遗传获得 (品质、身体特征等),接替 (责任等),继任
6. Example
//============================================================================
// Name : Yongqiang Cheng
// Author : Yongqiang Cheng
// Version : Version 1.0.0
// Copyright : Copyright (c) 2020 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <iostream>
#include <type_traits>
#include <cstdint>
typedef int integer_type;
struct A {
int x, y;
};
struct B {
int x, y;
};
typedef A C;
int main() {
std::cout << std::boolalpha;
std::cout << "is_same:" << std::endl;
std::cout << "int, const int: " << std::is_same<int, const int>::value << std::endl;
std::cout << "int, integer_type: " << std::is_same<int, integer_type>::value << std::endl;
std::cout << "A, B: " << std::is_same<A, B>::value << std::endl;
std::cout << "A, C: " << std::is_same<A, C>::value << std::endl;
std::cout << "signed char, std::int8_t: " << std::is_same<signed char, std::int8_t>::value << std::endl;
return 0;
}
is_same:
int, const int: false
int, integer_type: true
A, B: false
A, C: true
signed char, std::int8_t: true
请按任意键继续. . .
//============================================================================
// Name : Yongqiang Cheng
// Author : Yongqiang Cheng
// Version : Version 1.0.0
// Copyright : Copyright (c) 2020 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <iostream>
#include <type_traits>
#include <cstdint>
void print_separator() {
std::cout << "------" << std::endl;
}
int main()
{
std::cout << std::boolalpha;
// usually true if 'int' is 32 bit
std::cout << std::is_same<int, std::int32_t>::value << '\n'; // ~ true
// possibly true if ILP64 data model is used
std::cout << std::is_same<int, std::int64_t>::value << '\n'; // ~ false
print_separator();
// 'float' 决非整数类型
std::cout << std::is_same<float, std::int32_t>::value << '\n'; // false
print_separator();
// 'int' 为隐式的 'signed'
std::cout << std::is_same<int, int>::value << "\n"; // true
std::cout << std::is_same<int, unsigned int>::value << "\n"; // false
std::cout << std::is_same<int, signed int>::value << "\n"; // true
print_separator();
// 不同于其他类型, 'char' 既非 'unsigned' 亦非 'signed'
std::cout << std::is_same<char, char>::value << "\n"; // true
std::cout << std::is_same<char, unsigned char>::value << "\n"; // false
std::cout << std::is_same<char, signed char>::value << "\n"; // false
return 0;
}
true
false
------
false
------
true
false
true
------
true
false
false
请按任意键继续. . .
References
http://www.cplusplus.com/reference/type_traits/is_same/
https://zh.cppreference.com/w/cpp/types/is_same
https://en.cppreference.com/w/cpp/types/is_same