Type Trait

目录

1、Trait

2、函数

3、测试


1、Trait

提供处理类型属性的方法。

参考书籍《C++标准库》第二版;

2、函数

//类型判断
is_void<T>;                 是否是void类型
is_integral<T>;             是否是整数类型
is_floating_point<T>;       浮点整数类型 
is_arithmetic<T>;           整数或浮点数类型
is_signed<T>;               带符号算数类型
is_unsigned<T>;             无符号算数类型
is_const<T>;                只读
is_volatile<T>;             禁止优化
is_array<T>;                数组类型
is_enum<T>;                 枚举类型
is_union<T>;                联合类型
is_class<T>;                类类型
is_function<T>;             函数类型
is_reference<T>;            引用类型
is_lvalue_reference<T>;     左值或左值引用
is_rvalue_reference<T>;     右值或右值引用
is_pointer<T>;              指针
is_member_pointer<T>;       非静态成员指针
is_member_object_pointer<T>;       非静态成员数据指针
is_member_function_pointer<T>;     非静态成员函数指针
is_fundamental<T>;          void,整数,浮点,空
is_scalar<T>;               整数型 浮点型 枚举 指针 成员指针 空
is_object<T>;               任意类型;除了void、function、引用、数组、枚举、类、指针
is_compound<T>;             数组 枚举 联合体 类 函数 引用 指针
is_trivial<T>;              scalar、trivial或那些类型构成的数组
is_trivially_copyable<T>;   calar、trivial可复制的类或那些类型构成的数组
is_standard_layout<T>;      scalar、standard layout class或那些类型构成的数组
is_pod<T>;                  解释旧的数据类型(memcpy可用来复制的对象)。
is_literal_type<T>;         scalar、引用、class或那些类型构成的数组

//class类型判断
is_empty<T>;                 不带任何成员的类
is_polymorphic<T>;           带有一个 virtual成员函数
is_abstract<T>;              抽象类
has_virtual_destructor<T>;   带有一个virtual析构函数
is_default_constructible<T>; 默认构造
is_copy_constructible<T>;   复制构造
is_move_constructible<T>;   移动构造
is_copy_assignable<T>;      复制赋值
is_move_assignable<T>;      移动赋值
is_destructible<T>;         可调用的析构函数
is_trivially_default_constructible<T>; 
is_trivially_copy_constructible<T>;
is_trivially_move_constructible<T>;
is_trivially_copy_assignable<T>;
is_trivially_move_assignable<T>;
is_nothrow_default_constructible<T>;   默认构造且不抛异常
is_nothrow_copy_constructible<T>;      复制构造且不抛异常
is_nothrow_move_constructible<T>;      移动构造且不抛异常
is_nothrow_copy_assignable<T>;         复制赋值且不抛异常
is_nothrow_move_assignable<T>;         移动赋值且不抛异常
is_nothrow_destructible<T>;            析构且不抛异常

//检验类型关系
is_same<T1,T2>;                 t1和t2是相同类型
is_base_of<T1,T2>;              类型T1是类型T2的基础类
is_convertible<T1,T2>;          类型T1可转化为类型T2
is_constructible<T,Args...>;    可运用类型Args初始化T
is_trivially_constructible<T,Args...>;     以类型Args初始化T
is_nothrow_constructible<T,Args...>;       以类型Args初始化类型T而不抛异常
is_assignable<T1,T2>;           可将类型T2赋值给类型T1
is_nothrow_assignable<T1,T2>;   可将类型T2 赋值给类型T1
is_trivially_assignable<T1,T2>; 可将类型T2 赋值给类型T1而不抛异常
uses_allocator<T1,allc>;        allc可被转为T::allocator_type

//类型修饰 
remove_const<T>;             移除类型的const
remove_volatile<T>;          移除类型的volatile
remove_cv<T>;                移除类型的const和volatile
add_const<T>;                增加const
add_volatile<T>;             增加volatile
add_cv<T>;                   增加类型的const和volatile
make_signed<T>;              带正负号非引用类型
make_unsigned<T>;            无符号的非引用类型
remove_reference<T>;         无引用类型
add_lvalue_reference<T>;     增加左值引用类型
add_rvalue_reference<T>;     增加右值引用类型
remove_pointer<T>;           指针指向的类型
add_pointer<T>;              指针指向类型的无引用类型

//

3、测试

#include <iostream>
#include <type_traits>

using namespace std;

class CC
{
public:
	int m_iA;
	char m_cB;
};

int main()
{
	cout << boolalpha;
	int iLine = 18;
	//变量 类型判断
	cout << iLine++ << ends << is_void<int>::value << endl;
	cout << iLine++ << ends << is_integral<int>::value << endl;
	cout << iLine++ << ends << is_floating_point<int>::value << endl;
	cout << iLine++ << ends << is_arithmetic<int>::value << endl;
	cout << iLine++ << ends << is_signed<int>::value << endl;
	cout << iLine++ << ends << is_unsigned<int>::value << endl;
	cout << iLine++ << ends << is_const<int>::value << endl;
	cout << iLine++ << ends << is_volatile<int>::value << endl;
	cout << iLine++ << ends << is_array<int>::value << endl;
	cout << iLine++ << ends << is_enum<int>::value << endl;
	cout << iLine++ << ends << is_union<int>::value << endl;
	cout << iLine++ << ends << is_class<int>::value << endl;
	cout << iLine++ << ends << is_function<int>::value << endl;
	cout << iLine++ << ends << is_reference<int>::value << endl;
	cout << iLine++ << ends << is_lvalue_reference<int>::value << endl;
	cout << iLine++ << ends << is_rvalue_reference<int>::value << endl;
	cout << iLine++ << ends << is_pointer<int>::value << endl;
	cout << iLine++ << ends << is_member_pointer<int>::value << endl;
	cout << iLine++ << ends << is_member_object_pointer<int>::value << endl;
	cout << iLine++ << ends << is_member_function_pointer<int>::value << endl;
	cout << iLine++ << ends << is_fundamental<int>::value << endl;
	cout << iLine++ << ends << is_scalar<int>::value << endl;
	cout << iLine++ << ends << is_object<int>::value << endl;
	cout << iLine++ << ends << is_compound<int>::value << endl;
	cout << iLine++ << ends << is_trivial<int>::value << endl;
	cout << iLine++ << ends << is_trivially_copyable<int>::value << endl;
	cout << iLine++ << ends << is_standard_layout<int>::value << endl;
	cout << iLine++ << ends << is_pod<int>::value << endl;
	cout << iLine++ << ends << is_literal_type<int>::value << endl;

	iLine = 50;
	//类类型判断
	cout << iLine++ << ends << is_empty<CC>::value << endl;
	cout << iLine++ << ends << is_polymorphic<CC>::value << endl;
	cout << iLine++ << ends << is_abstract<CC>::value << endl;
	cout << iLine++ << ends << has_virtual_destructor<CC>::value << endl;
	cout << iLine++ << ends << is_default_constructible<CC>::value << endl;
	cout << iLine++ << ends << is_copy_constructible<CC>::value << endl;
	cout << iLine++ << ends << is_move_constructible<CC>::value << endl;
	cout << iLine++ << ends << is_copy_assignable<CC>::value << endl;
	cout << iLine++ << ends << is_move_assignable<CC>::value << endl;
	cout << iLine++ << ends << is_destructible<CC>::value << endl;
	cout << iLine++ << ends << is_trivially_default_constructible<CC>::value << endl;
	cout << iLine++ << ends << is_trivially_copy_constructible<CC>::value << endl;
	cout << iLine++ << ends << is_trivially_move_constructible<CC>::value << endl;
	cout << iLine++ << ends << is_trivially_copy_assignable<CC>::value << endl;
	cout << iLine++ << ends << is_trivially_move_assignable<CC>::value << endl;
	cout << iLine++ << ends << is_nothrow_default_constructible<CC>::value << endl;
	cout << iLine++ << ends << is_nothrow_copy_constructible<CC>::value << endl;
	cout << iLine++ << ends << is_nothrow_move_constructible<CC>::value << endl;
	cout << iLine++ << ends << is_nothrow_copy_assignable<CC>::value << endl;
	cout << iLine++ << ends << is_nothrow_move_assignable<CC>::value << endl;
	cout << iLine++ << ends << is_nothrow_destructible<CC>::value << endl;

	//用以检验类型关系
	cout << is_same<int, char>::value << endl;
	cout << is_base_of<int, char>::value << endl;
	cout << is_convertible<int, char>::value << endl;
	//cout << is_constructible<T, Args...>::value << endl;
	//cout << is_trivially_constructible<T, Args...>::value << endl;
	//cout << is_nothrow_constructible<T, Args...>::value << endl;
	cout << is_assignable<int, char>::value << endl;
	//cout << is_nothrow_assignable<T, Args...>::value << endl;
	//cout << is_trivially_assignable<T, Args...>::value << endl;
	//cout << uses_allocator<T, allc>::value << endl;

	//类型修饰符 为类型添加属性(该属性不存在),为类型移除属性(该属性已存在)
	//直接创建对应类型的变量
	remove_const<CC>::type A;
	remove_volatile<CC>::type B;
	remove_cv<CC>::type C;
	add_const<int>::type D = 1;
	add_volatile<int>::type E;
	add_cv<int>::type F = 2;
	make_signed<int>::type G;
	make_unsigned<int>::type H; 
	remove_reference<int>::type I;
	add_lvalue_reference<int>::type J = I;
	int itemp = 10;
	add_rvalue_reference<int>::type K = move(itemp);
	remove_pointer<int>::type L;
	add_pointer<int>::type M;

	//检查类型关系和复杂类型变化
	typedef int array[10];
	rank<array>::value;
	extent<array, 0>::value;
	//remove_extent<array>::type;
	//remove_all_extents<array>::type;
	//underlying_type<array>::type;
	//decay<array>::type;
	//enable_if<true, void>::type;
	//conditional<true, array, void>::type;
	//common_type<T1...>
	//result_of...
	//alignment_of<array>::value;
	//aligned_storage<len>
	//aligned_storage<len, align>;
	//aligned_union<len,types...>
	system("pause");
}
发布了44 篇原创文章 · 获赞 54 · 访问量 7031

猜你喜欢

转载自blog.csdn.net/Liu_Xiao_Ming/article/details/104236523