//type trait 之类型判断工具
#include <iostream>
#include <array>
using namespace std;
enum class color
{
red,green,blue
};
auto f = []()
{
cout << "I'm lambda" << endl;
};
union A
{
int a;
float b;
string c;
};
class B
{
public:
int b;
void f() {}
};
int main()
{
//is void?
cout << boolalpha << is_void<void>::value << endl;
//整数类型包括(bool char int char16_t char32_t wchar_t)?
cout << boolalpha << is_integral<int>::value << endl;
//浮点数(float double long double)?
cout << boolalpha << is_floating_point<int>::value << endl;
//算术类型(包括整数和浮点数)?arithmetic
cout << boolalpha << is_arithmetic<string>::value << endl;
//带正负号的arithmetic type
cout << boolalpha << is_signed<string>::value << endl;
//不带正负号的arithmetic type
cout << boolalpha << is_unsigned<int>::value << endl;
//对象是否带有const限定符
cout << boolalpha << is_const<const int*>::value << endl;
//对象是否带有volatile限定符
cout << boolalpha << is_volatile<volatile int * volatile>::value << endl;
//row array ?
cout << boolalpha << is_array<array<int,5>>::value << endl;
// enum ?
cout << boolalpha << is_enum<color>::value << endl;
// union ?
cout << boolalpha << is_union<A>::value << endl;
// class(class or struct but nou union) ?
cout << boolalpha << is_class<A>::value << endl;
//function type?
cout << boolalpha << is_function<decltype(f)()>::value << endl;
// reference ?
cout << boolalpha << is_reference<int&&>::value << endl;
// left reference ?
cout << boolalpha << is_lvalue_reference<int&&>::value << endl;
// right reference ?
cout << boolalpha << is_rvalue_reference<int&&>::value << endl;
// row pointer ?
cout << is_pointer<int*>::value << endl;
//pointer to nonstatic 成员 ?
cout << is_member_pointer<decltype(&B::f)>::value << endl;
//pointer to nonstatic 数据成员
cout << is_member_object_pointer<decltype(&B::f)>::value << endl;
//pointer to nonstatic 成员函数
cout << is_member_function_pointer<decltype(&B::f)>::value << endl;
//包括(整型,void,浮点型,std::nullptr_t)
cout << is_fundamental<string>::value << endl;
//整型 浮点数 枚举(enumeration) pointer,member pointer,std::nullptr_t
cout << is_scalar<int>::value << endl;
//任何类型除void reference pointer function array enumeration union
//class
cout << is_object<string>::value << endl;
system("pause");
return 0;
}
c++ type trait 之 类型判断工具
最新推荐文章于 2024-07-31 15:18:44 发布