当前位置:网站首页>type_traits元编程库学习
type_traits元编程库学习
2022-07-31 03:21:00 【班公湖里洗过脚】
type_traits是元编程库的一部分,这个库主要用来判断数据类型,比如,判断类型是否为空,是否为空指针,是否为整型,是否为浮点型是否为数组,是否为枚举类型,是否为联合体,是否为函数,是否为指针,是否为左值引用,是否为右值引用,等等,判断的类型非常多,本篇介绍几个基本的。
基础类型分类 | |
(C++11) | 检查类型是否为 void (类模板) |
(C++14) | 检查类型是否为 std::nullptr_t (类模板) |
(C++11) | 检查类型是否为整型 (类模板) |
(C++11) | 检查类型是否是浮点类型 (类模板) |
(C++11) | 检查类型是否是数组类型 (类模板) |
(C++11) | 检查类型是否是枚举类型 (类模板) |
(C++11) | 检查类型是否为联合体类型 (类模板) |
(C++11) | 检查类型是否非联合类类型 (类模板) |
(C++11) | 检查是否为函数类型 (类模板) |
(C++11) | 检查类型是否为指针类型 (类模板) |
(C++11) | 检查类型是否为左值引用 (类模板) |
(C++11) | 检查类型是否为右值引用 (类模板) |
(C++11) | 检查类型是否为指向非静态成员对象的指针 (类模板) |
(C++11) | 检查类型是否为指向非静态成员函数的指针 (类模板) |
1. is_void 检查类型是否为 void
#include <iostream>
#include <type_traits>
using namespace std;
int main()
{
cout << "std::is_void<void>::value==========" << std::is_void<void>::value << endl;
cout << "std::is_void<int>::value===========" << std::is_void<int>::value << endl;
cout << "Hello World!" << endl;
return 0;
}
运行结果:

2. is_null_pointer 检查类型是否为 std::nullptr_t
#include <iostream>
#include <type_traits>
using namespace std;
int main()
{
cout << "std::is_null_pointer<decltype(nullptr)>::value = " << is_null_pointer<decltype(nullptr)>::value << endl;
cout << "std::is_null_pointer<int*>::value = " << is_null_pointer<int*>::value << endl;
cout << "Hello World!" << endl;
return 0;
}
运行结果:

3. is_integral 检查类型是否为整型
#include <iostream>
#include <type_traits>
using namespace std;
class A {};
enum E: int {};
template <class T>
T fn(T i)
{
static_assert (std::is_integral<T>::value, "Integral required.");
return i;
}
int main()
{
cout << "std::is_integral<A>::value======= " << std::is_integral<A>::value << endl;
cout << "std::is_integral<E>::value======= " << std::is_integral<E>::value << endl;
cout << "std::is_integral<float>::value=== " << std::is_integral<float>::value << endl;
cout << "std::is_integral<int>::value===== " << std::is_integral<int>::value << endl;
cout << "std::is_integral<bool>::value==== " << std::is_integral<bool>::value << endl;
cout << "fn(123)========================== " << fn(123) << endl;
//cout << "fn(123.5)======================== " << fn(123.5) << endl; //编译失败
cout << "Hello World!" << endl;
return 0;
}
运行结果:

4. is_floating_point 检查类型是否是浮点类型
#include <iostream>
#include <type_traits>
using namespace std;
class A {};
int main()
{
cout << "std::is_floating_point<A>::value====== " << std::is_floating_point<A>::value << endl;
cout << "std::is_floating_point<float>::value== " << std::is_floating_point<float>::value << endl;
cout << "std::is_floating_point<float>::value== " << std::is_floating_point<float&>::value << endl;
cout << "std::is_floating_point<double>::value= " << std::is_floating_point<double>::value << endl;
cout << "std::is_floating_point<double>::value= " << std::is_floating_point<double&>::value << endl;
cout << "std::is_floating_point<int>::value==== " << std::is_floating_point<int>::value << endl;
cout << "Hello World!" << endl;
return 0;
}
运行结果:

5. is_array 检查类型是否是数组类型
#include <iostream>
#include <type_traits>
using namespace std;
class A {};
int main()
{
cout << "std::is_array<A>::value============ " << std::is_array<A>::value << endl;
cout << "std::is_array<A[]>::value========== " << std::is_array<A[]>::value << endl;
cout << "std::is_array<A[3]>::value========= " << std::is_array<A[3]>::value << endl;
cout << "std::is_array<float>::value======== " << std::is_array<float>::value << endl;
cout << "std::is_array<int>::value========== " << std::is_array<int>::value << endl;
cout << "std::is_array<int[]>::value======== " << std::is_array<int[]>::value << endl;
cout << "std::is_array<int[3]>::value======= " << std::is_array<int[3]>::value << endl;
cout << "std::is_array<array<int,3>>::value= " << std::is_array<array<int,3>>::value << endl;
cout << "Hello World!" << endl;
return 0;
}
运行结果:

6. is_enum 检查类型是否是枚举类型
#include <iostream>
#include <type_traits>
using namespace std;
class A {};
enum E {};
enum class Ec: int {};
int main()
{
cout << "std::is_enum<A>::value====== " << std::is_enum<A>::value << endl;
cout << "std::is_enum<E>::value====== " << std::is_enum<E>::value << endl;
cout << "std::is_enum<Ec>::value===== " << std::is_enum<Ec>::value << endl;
cout << "std::is_enum<int>::value==== " << std::is_enum<int>::value << endl;
cout << "Hello World!" << endl;
return 0;
}
运行结果:

7. is_union 检查类型是否为联合体类型
#include <iostream>
#include <type_traits>
using namespace std;
class A {};
typedef union {
int a;
float b;
} B;
class C {
B d;
};
int main()
{
cout << "std::is_union<A>::value======== " << std::is_union<A>::value << endl;
cout << "std::is_union<B>::value======== " << std::is_union<B>::value << endl;
cout << "std::is_union<C>::value======== " << std::is_union<C>::value << endl;
cout << "std::is_union<int>::value====== " << std::is_union<int>::value << endl;
cout << "Hello World!" << endl;
return 0;
}
运行结果:

8. is_class 检查类型是否非联合类类型
#include <iostream>
#include <type_traits>
using namespace std;
struct A { };
class B { } ;
enum class C { };
int main()
{
cout << "std::is_class<A>::value========== " << std::is_class<A>::value << endl;
cout << "std::is_class<B>::value========== " << std::is_class<B>::value << endl;
cout << "std::is_class<C>::value========== " << std::is_class<C>::value << endl;
cout << "std::is_class<int>::value======== " << std::is_class<int>::value << endl;
cout << "Hello World!" << endl;
return 0;
}运行结果:

9. is_function 检查是否为函数类型
#include <iostream>
#include <type_traits>
using namespace std;
struct A {
int fun() const &;
};
template<typename>
struct PM_traits { };
template<class T, class U>
struct PM_traits<U T::*> {
using member_type = U;
};
int f();
int main()
{
cout << "std::is_function<A>::value================= " << std::is_function<A>::value << endl;
cout << "std::is_function<int(int)>::value========== " << std::is_function<int(int)>::value << endl;
cout << "std::is_function<decltype(f)>::value======= " << std::is_function<decltype(f)>::value << endl;
cout << "std::is_function<int>::value=============== " << std::is_function<int>::value << endl;
using T = PM_traits<decltype(&A::fun)>::member_type; //T为int() const&
cout << "std::is_function<T>::value================= " << std::is_function<T>::value << endl;
cout << "Hello World!" << endl;
return 0;
}
运行结果:

10. is_pointer 检查类型是否为指针类型
#include <iostream>
#include <type_traits>
using namespace std;
struct A {
int fun() const &;
};
int main()
{
cout << "std::is_pointer<A>::value ================= " << is_pointer<A>::value << endl;
cout << "std::is_pointer<A *>::value =============== " << is_pointer<A *>::value << endl;
cout << "std::is_pointer<A &>::value =============== " << is_pointer<A &>::value << endl;
cout << "std::is_pointer<int>::value =============== " << is_pointer<int>::value << endl;
cout << "std::is_pointer<int *>::value ============= " << is_pointer<int *>::value << endl;
cout << "std::is_pointer<int **>::value ============ " << is_pointer<int **>::value << endl;
cout << "std::is_pointer<int[10]>::value =========== " << is_pointer<int[10]>::value << endl;
cout << "std::is_pointer<nullptr_t>::value ========= " << is_pointer<nullptr_t>::value << endl;
cout << "std::is_pointer<decltype(nullptr)>::value = " << is_pointer<decltype(nullptr)>::value << endl;
cout << "Hello World!" << endl;
return 0;
}
运行结果:

11. is_lvalue_reference 检查类型是否为左值引用
#include <iostream>
#include <type_traits>
using namespace std;
class A { };
int main()
{
cout << "std::is_lvalue_reference<A>::value====== " << std::is_lvalue_reference<A>::value << endl;
cout << "std::is_lvalue_reference<A&>::value===== " << std::is_lvalue_reference<A&>::value << endl;
cout << "std::is_lvalue_reference<A&&>::value==== " << std::is_lvalue_reference<A&&>::value << endl;
cout << "std::is_lvalue_reference<int>::value==== " << std::is_lvalue_reference<int>::value << endl;
cout << "std::is_lvalue_reference<int&>::value=== " << std::is_lvalue_reference<int&>::value << endl;
cout << "std::is_lvalue_reference<int&&>::value== " << std::is_lvalue_reference<int&&>::value << endl;
cout << "Hello World!" << endl;
return 0;
}
运行结果:

12. is_rvalue_reference 检查类型是否为右值引用
#include <iostream>
#include <type_traits>
using namespace std;
class A { };
int main()
{
cout << "std::is_rvalue_reference<A>::value====== " << std::is_rvalue_reference<A>::value << endl;
cout << "std::is_rvalue_reference<A&>::value===== " << std::is_rvalue_reference<A&>::value << endl;
cout << "std::is_rvalue_reference<A&&>::value==== " << std::is_rvalue_reference<A&&>::value << endl;
cout << "std::is_rvalue_reference<int>::value==== " << std::is_rvalue_reference<int>::value << endl;
cout << "std::is_rvalue_reference<int&>::value=== " << std::is_rvalue_reference<int&>::value << endl;
cout << "std::is_rvalue_reference<int&&>::value== " << std::is_rvalue_reference<int&&>::value << endl;
cout << "Hello World!" << endl;
return 0;
}
运行结果:

13. is_member_object_pointer 检查类型是否为指向非静态成员对象的指针
#include <iostream>
#include <type_traits>
using namespace std;
int main()
{
class cls {};
std::cout << (std::is_member_object_pointer<int(cls::*)>::value
? "T is member object pointer"
: "T is not a member object pointer") << endl;
std::cout << (std::is_member_object_pointer<int(cls::*)()>::value
? "T is member object pointer"
: "T is not a member object pointer") << endl;
cout << "Hello World!" << endl;
return 0;
}
运行结果:

14. is_member_function_pointer 检查类型是否为指向非静态成员函数的指针
#include <iostream>
#include <type_traits>
using namespace std;
class A {
public:
void member() { }
};
int main()
{
// 若 A::member 是数据成员而非函数,则在编译时失败
static_assert(std::is_member_function_pointer<decltype(&A::member)>::value,
"A::member is not a member function.");
cout << "Hello World!" << endl;
return 0;
}
参考:
边栏推荐
- 【Cocos Creator 3.5】缓动系统停止所有动画
- 接口测试关键技术
- VS QT - ui does not display newly added members (controls) || code is silent
- els 方块向右移
- 刚出道“一战成名”,安全、舒适一个不落
- Modbus on AT32 MCUs
- 【AUTOSAR-RTE】-4-Port和Interface以及Data Type
- 【AUTOSAR-RTE】-5-Explicit(显式)和Implicit(隐式) Sender-Receiver communication
- $parent/$children 与 ref
- Automation strategies for legacy systems
猜你喜欢
随机推荐
What is a distributed lock?Three ways of implementing distributed lock
Mysql 45 study notes (23) How does MYSQL ensure that data is not lost
安全20220715
TCP详解(一)
False positives and false negatives in testing are equally worthy of repeated corrections
【编译原理】词法分析程序设计原理与实现
endian mode
浅识Flutter 基本组件之CheckboxListTile组件
Addition and Subtraction of Scores in LeetCode Medium Questions
How to develop a high-quality test case?
What skills do I need to learn to move from manual testing to automated testing?
Implementation of a sequence table
Compile Hudi
[C language] General method for finding the sum of the greatest common factor and the least common multiple of two integers m and n, the classical solution
接口测试关键技术
LeetCode simple problem to find the subsequence of length K with the largest sum
[C language] Preprocessing operation
LocalDate加减操作及比较大小
顺序表的实现
Distributed locks and three implementation methods









