当前位置:网站首页>type_traits metaprogramming library learning
type_traits metaprogramming library learning
2022-07-31 03:32:00 【Wash your feet in Pangong Lake】
type_traitsis part of the metaprogramming library,This library is mainly used to determine the data type,比如,判断类型是否为空,是否为空指针,是否为整型,Whether it is a float or not whether it is an array,Is it an enumeration type,Is it a union,是否为函数,是否为指针,Is it an lvalue reference,Is it an rvalue reference,等等,There are many types of judgments,This article introduces a few basic ones.
Basic type classification | |
(C++11) | 检查类型是否为 void (类模板) |
(C++14) | 检查类型是否为 std::nullptr_t (类模板) |
(C++11) | Check if the type is an integer (类模板) |
(C++11) | Check if the type is a floating point type (类模板) |
(C++11) | Check if the type is an array type (类模板) |
(C++11) | Check if the type is an enumeration type (类模板) |
(C++11) | Check if the type is a union type (类模板) |
(C++11) | Check if the type is not a union class type (类模板) |
(C++11) | Check if it is a function type (类模板) |
(C++11) | Check if the type is a pointer type (类模板) |
(C++11) | 检查类型是否为左值引用 (类模板) |
(C++11) | 检查类型是否为右值引用 (类模板) |
(C++11) | Checks if the type is a pointer to a non-static member object (类模板) |
(C++11) | Checks if the type is a pointer to a non-static member function (类模板) |
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 Check if the type is an integer
#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 Check if the type is a floating point type
#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 Check if the type is an array type
#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 Check if the type is an enumeration type
#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 Check if the type is a union type
#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 Check if the type is not a union class type
#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 Check if it is a function type
#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 Check if the type is a pointer type
#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 Checks if the type is an 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 Checks if the type is an 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 Checks if the type is a pointer to a non-static member object
#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 Checks if the type is a pointer to a non-static member function
#include <iostream>
#include <type_traits>
using namespace std;
class A {
public:
void member() { }
};
int main()
{
// 若 A::member is a data member not a function,fails at compile time
static_assert(std::is_member_function_pointer<decltype(&A::member)>::value,
"A::member is not a member function.");
cout << "Hello World!" << endl;
return 0;
}
参考:
边栏推荐
- False positives and false negatives in testing are equally worthy of repeated corrections
- 分布式系统架构需要解决的问题
- RESTful api接口设计规范
- 浅识Flutter 基本组件之CheckBox组件
- $attrs/$listeners
- [Godot][GDScript] 2D cave map randomly generated
- A brief introduction to the CheckBox component of the basic components of Flutter
- Addition and Subtraction of Scores in LeetCode Medium Questions
- SIP协议标准和实现机制
- 【HCIP】ISIS
猜你喜欢
Detailed explanation of TCP and UDP
type_traits元编程库学习
安全20220712
[C language] Preprocessing operation
[Compilation principle] Lexical analysis program design principle and implementation
[Dynamic programming] Maximum sum of consecutive subarrays
立足本土,链接全球 | 施耐德电气“工业SI同盟”携手伙伴共赴未来工业
A brief introduction to the CheckboxListTile component of the basic components of Flutter
IDEA常用快捷键与插件
$attrs/$listeners
随机推荐
Difference between unallocated blocks and unused blocks in database files
(树) 最近公共祖先(LCA)
STM32 problem collection
7年经验,功能测试工程师该如何一步步提升自己的能力呢?
端口排查步骤-7680端口分析-Dosvc服务
【C语言】预处理操作
测试中的误报和漏报同样的值得反复修正
SQL Interview Questions (Key Points)
TCP详解(一)
Redis 使用 sorted set 做最新评论缓存
【论文阅读】Mastering the game of Go with deep neural networks and tree search
【编译原理】递归下降语法分析设计原理与实现
Redis 使用LIST做最新评论缓存
Good place to download jar packages
安全20220715
Point Cloud DBSCAN Clustering (MATLAB, not built-in function)
SocialFi 何以成就 Web3 去中心化社交未来
选好冒烟测试用例,为进入QA的制品包把好第一道关
endian mode
Web container and IIS --- Middleware penetration method 1