C ++中的is_signed模板
在本文中,我们将讨论C++STL中std::is_signed模板的工作,语法和示例。
is_signed是位于<type_traits>头文件下的模板。该模板用于检查给定类型T是否为带符号类型。
什么是带符号类型?
这些是基本的算术类型,其中包含符号值。所有算术数据类型均为带符号和无符号。
就像我们要显示负值一样,我们使用带符号的类型。
像:-1是有符号的int,而-1.009是有符号的float。
默认情况下,所有类型都是带符号的,以使它们成为无符号的,我们必须在数据类型前加上无符号的前缀。
语法
template <class T> is_signed;
参数
模板只能具有类型T的参数,并检查T是否为带符号类型。
返回值
它返回一个布尔值,如果给定类型为Signed类型,则返回true;如果给定类型不是Signed类型,则返回false。
示例
Input: is_signed<int>::value; Output: True Input: is_signed<unsigned int>::value; Output: False
示例
#include <iostream>
#include <type_traits>
using namespace std;
class TP {
};
enum TP_1 : int {};
enum class TP_2 : int {};
int main() {
cout << boolalpha;
cout << "checking for is_signed:";
cout << "\nint:" << is_signed<int>::value;
cout << "\nTP:" << is_signed<TP>::value;
cout << "\nTP_1:" << is_signed<TP_1>::value;
cout << "\nTP_2:" << is_signed<TP_2>::value;
return 0;
}输出结果
如果我们运行上面的代码,它将生成以下输出-
checking for is_signed: Int: true TP: false TP_1: false TP_2: false
示例
#include <iostream>
#include <type_traits>
using namespace std;
int main() {
cout << boolalpha;
cout << "checking for is_signed:";
cout << "\nfloat:" << is_signed<float>::value;
cout << "\nSigned int:" << is_signed<signed int>::value;
cout << "\nUnsigned int:" << is_signed<unsigned int>::value;
cout << "\ndouble:" << is_signed<double>::value;
return 0;
}输出结果
如果我们运行上面的代码,它将生成以下输出-
checking for is_signed: Float: true Signed int: true Unsigned int: false Double: true
热门推荐
10 八一幼儿祝福语大全简短
11 公司乔迁食堂祝福语简短
12 婚礼结束聚餐祝福语简短
13 儿媳买车妈妈祝福语简短
14 毕业送礼老师祝福语简短
15 同事辞职正常祝福语简短
16 恭贺新婚文案祝福语简短
17 金店立秋祝福语简短英文
18 婆婆高寿祝福语大全简短