目录
1、Trait
2、函数
3、测试
提供处理类型属性的方法。
参考书籍《C++标准库》第二版;
//类型判断
is_void; 是否是void类型
is_integral; 是否是整数类型
is_floating_point; 浮点整数类型
is_arithmetic; 整数或浮点数类型
is_signed; 带符号算数类型
is_unsigned; 无符号算数类型
is_const; 只读
is_volatile; 禁止优化
is_array; 数组类型
is_enum; 枚举类型
is_union; 联合类型
is_class; 类类型
is_function; 函数类型
is_reference; 引用类型
is_lvalue_reference; 左值或左值引用
is_rvalue_reference; 右值或右值引用
is_pointer; 指针
is_member_pointer; 非静态成员指针
is_member_object_pointer; 非静态成员数据指针
is_member_function_pointer; 非静态成员函数指针
is_fundamental; void,整数,浮点,空
is_scalar; 整数型 浮点型 枚举 指针 成员指针 空
is_object; 任意类型;除了void、function、引用、数组、枚举、类、指针
is_compound; 数组 枚举 联合体 类 函数 引用 指针
is_trivial; scalar、trivial或那些类型构成的数组
is_trivially_copyable; calar、trivial可复制的类或那些类型构成的数组
is_standard_layout; scalar、standard layout class或那些类型构成的数组
is_pod; 解释旧的数据类型(memcpy可用来复制的对象)。
is_literal_type; scalar、引用、class或那些类型构成的数组
//class类型判断
is_empty; 不带任何成员的类
is_polymorphic; 带有一个 virtual成员函数
is_abstract; 抽象类
has_virtual_destructor; 带有一个virtual析构函数
is_default_constructible; 默认构造
is_copy_constructible; 复制构造
is_move_constructible; 移动构造
is_copy_assignable; 复制赋值
is_move_assignable; 移动赋值
is_destructible; 可调用的析构函数
is_trivially_default_constructible;
is_trivially_copy_constructible;
is_trivially_move_constructible;
is_trivially_copy_assignable;
is_trivially_move_assignable;
is_nothrow_default_constructible; 默认构造且不抛异常
is_nothrow_copy_constructible; 复制构造且不抛异常
is_nothrow_move_constructible; 移动构造且不抛异常
is_nothrow_copy_assignable; 复制赋值且不抛异常
is_nothrow_move_assignable; 移动赋值且不抛异常
is_nothrow_destructible; 析构且不抛异常
//检验类型关系
is_same; t1和t2是相同类型
is_base_of; 类型T1是类型T2的基础类
is_convertible; 类型T1可转化为类型T2
is_constructible; 可运用类型Args初始化T
is_trivially_constructible; 以类型Args初始化T
is_nothrow_constructible; 以类型Args初始化类型T而不抛异常
is_assignable; 可将类型T2赋值给类型T1
is_nothrow_assignable; 可将类型T2 赋值给类型T1
is_trivially_assignable; 可将类型T2 赋值给类型T1而不抛异常
uses_allocator; allc可被转为T::allocator_type
//类型修饰
remove_const; 移除类型的const
remove_volatile; 移除类型的volatile
remove_cv; 移除类型的const和volatile
add_const; 增加const
add_volatile; 增加volatile
add_cv; 增加类型的const和volatile
make_signed; 带正负号非引用类型
make_unsigned; 无符号的非引用类型
remove_reference; 无引用类型
add_lvalue_reference; 增加左值引用类型
add_rvalue_reference; 增加右值引用类型
remove_pointer; 指针指向的类型
add_pointer; 指针指向类型的无引用类型
//
#include
#include
using namespace std;
class CC
{
public:
int m_iA;
char m_cB;
};
int main()
{
cout << boolalpha;
int iLine = 18;
//变量 类型判断
cout << iLine++ << ends << is_void::value << endl;
cout << iLine++ << ends << is_integral::value << endl;
cout << iLine++ << ends << is_floating_point::value << endl;
cout << iLine++ << ends << is_arithmetic::value << endl;
cout << iLine++ << ends << is_signed::value << endl;
cout << iLine++ << ends << is_unsigned::value << endl;
cout << iLine++ << ends << is_const::value << endl;
cout << iLine++ << ends << is_volatile::value << endl;
cout << iLine++ << ends << is_array::value << endl;
cout << iLine++ << ends << is_enum::value << endl;
cout << iLine++ << ends << is_union::value << endl;
cout << iLine++ << ends << is_class::value << endl;
cout << iLine++ << ends << is_function::value << endl;
cout << iLine++ << ends << is_reference::value << endl;
cout << iLine++ << ends << is_lvalue_reference::value << endl;
cout << iLine++ << ends << is_rvalue_reference::value << endl;
cout << iLine++ << ends << is_pointer::value << endl;
cout << iLine++ << ends << is_member_pointer::value << endl;
cout << iLine++ << ends << is_member_object_pointer::value << endl;
cout << iLine++ << ends << is_member_function_pointer::value << endl;
cout << iLine++ << ends << is_fundamental::value << endl;
cout << iLine++ << ends << is_scalar::value << endl;
cout << iLine++ << ends << is_object::value << endl;
cout << iLine++ << ends << is_compound::value << endl;
cout << iLine++ << ends << is_trivial::value << endl;
cout << iLine++ << ends << is_trivially_copyable::value << endl;
cout << iLine++ << ends << is_standard_layout::value << endl;
cout << iLine++ << ends << is_pod::value << endl;
cout << iLine++ << ends << is_literal_type::value << endl;
iLine = 50;
//类类型判断
cout << iLine++ << ends << is_empty::value << endl;
cout << iLine++ << ends << is_polymorphic::value << endl;
cout << iLine++ << ends << is_abstract::value << endl;
cout << iLine++ << ends << has_virtual_destructor::value << endl;
cout << iLine++ << ends << is_default_constructible::value << endl;
cout << iLine++ << ends << is_copy_constructible::value << endl;
cout << iLine++ << ends << is_move_constructible::value << endl;
cout << iLine++ << ends << is_copy_assignable::value << endl;
cout << iLine++ << ends << is_move_assignable::value << endl;
cout << iLine++ << ends << is_destructible::value << endl;
cout << iLine++ << ends << is_trivially_default_constructible::value << endl;
cout << iLine++ << ends << is_trivially_copy_constructible::value << endl;
cout << iLine++ << ends << is_trivially_move_constructible::value << endl;
cout << iLine++ << ends << is_trivially_copy_assignable::value << endl;
cout << iLine++ << ends << is_trivially_move_assignable::value << endl;
cout << iLine++ << ends << is_nothrow_default_constructible::value << endl;
cout << iLine++ << ends << is_nothrow_copy_constructible::value << endl;
cout << iLine++ << ends << is_nothrow_move_constructible::value << endl;
cout << iLine++ << ends << is_nothrow_copy_assignable::value << endl;
cout << iLine++ << ends << is_nothrow_move_assignable::value << endl;
cout << iLine++ << ends << is_nothrow_destructible::value << endl;
//用以检验类型关系
cout << is_same::value << endl;
cout << is_base_of::value << endl;
cout << is_convertible::value << endl;
//cout << is_constructible::value << endl;
//cout << is_trivially_constructible::value << endl;
//cout << is_nothrow_constructible::value << endl;
cout << is_assignable::value << endl;
//cout << is_nothrow_assignable::value << endl;
//cout << is_trivially_assignable::value << endl;
//cout << uses_allocator::value << endl;
//类型修饰符 为类型添加属性(该属性不存在),为类型移除属性(该属性已存在)
//直接创建对应类型的变量
remove_const::type A;
remove_volatile::type B;
remove_cv::type C;
add_const::type D = 1;
add_volatile::type E;
add_cv::type F = 2;
make_signed::type G;
make_unsigned::type H;
remove_reference::type I;
add_lvalue_reference::type J = I;
int itemp = 10;
add_rvalue_reference::type K = move(itemp);
remove_pointer::type L;
add_pointer::type M;
//检查类型关系和复杂类型变化
typedef int array[10];
rank::value;
extent::value;
//remove_extent::type;
//remove_all_extents::type;
//underlying_type::type;
//decay::type;
//enable_if::type;
//conditional::type;
//common_type
//result_of...
//alignment_of::value;
//aligned_storage
//aligned_storage;
//aligned_union
system("pause");
}