C++ 语言 std::is_same

本文详细介绍了C++中std::is_same模板类的作用,用于判断两个类型是否相同,包括const和volatile修饰的情况。通过示例展示了其在不同类型的比较中的应用,如int与const int、自定义结构体之间的比较等。此外,还提到了std::is_same的成员常量value,用于获取比较结果。
摘要由CSDN通过智能技术生成

C++ 语言 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 限定。

TU 为同一类型 (考虑 const/volatile 限定),则提供等于 true 的成员常量 value 。否则 value 为 false 。满足交换律,即对于任何二个类型 TUis_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_sameintegral_constant 继承 true_typefalse_type,取决于 TU 是否是相同的类型。

继承自 std::integral_constant

2. Template parameters

T, U: Types.

3. Member types - 成员类型

Inherited from integral_constant:

member typedefinition
value_typebool
typeeither true_type or false_type

4. Member constants - 成员常量

Inherited from integral_constant:

member constantdefinition
valueeither true or false

TU 是同一类型则为 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Yongqiang Cheng

梦想不是浮躁,而是沉淀和积累。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值