码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • type_traits元编程库学习


    type_traits是元编程库的一部分,这个库主要用来判断数据类型,比如,判断类型是否为空,是否为空指针,是否为整型,是否为浮点型是否为数组,是否为枚举类型,是否为联合体,是否为函数,是否为指针,是否为左值引用,是否为右值引用,等等,判断的类型非常多,本篇介绍几个基本的。

    基础类型分类

    is_void

    (C++11)

    检查类型是否为 void
    (类模板)

    is_null_pointer

    (C++14)

    检查类型是否为 std::nullptr_t
    (类模板)

    is_integral

    (C++11)

    检查类型是否为整型
    (类模板)

    is_floating_point

    (C++11)

    检查类型是否是浮点类型
    (类模板)

    is_array

    (C++11)

    检查类型是否是数组类型
    (类模板)

    is_enum

    (C++11)

    检查类型是否是枚举类型
    (类模板)

    is_union

    (C++11)

    检查类型是否为联合体类型
    (类模板)

    is_class

    (C++11)

    检查类型是否非联合类类型
    (类模板)

    is_function

    (C++11)

    检查是否为函数类型
    (类模板)

    is_pointer

    (C++11)

    检查类型是否为指针类型
    (类模板)

    is_lvalue_reference

    (C++11)

    检查类型是否为左值引用
    (类模板)

    is_rvalue_reference

    (C++11)

    检查类型是否为右值引用
    (类模板)

    is_member_object_pointer

    (C++11)

    检查类型是否为指向非静态成员对象的指针
    (类模板)

    is_member_function_pointer

    (C++11)

    检查类型是否为指向非静态成员函数的指针
    (类模板)

    1. is_void    检查类型是否为 void

    1. #include
    2. #include
    3. using namespace std;
    4. int main()
    5. {
    6. cout << "std::is_void::value==========" << std::is_void<void>::value << endl;
    7. cout << "std::is_void::value===========" << std::is_void<int>::value << endl;
    8. cout << "Hello World!" << endl;
    9. return 0;
    10. }

    运行结果:

    2. is_null_pointer    检查类型是否为 std::nullptr_t

    1. #include
    2. #include
    3. using namespace std;
    4. int main()
    5. {
    6. cout << "std::is_null_pointer::value = " << is_null_pointer<decltype(nullptr)>::value << endl;
    7. cout << "std::is_null_pointer::value = " << is_null_pointer<int*>::value << endl;
    8. cout << "Hello World!" << endl;
    9. return 0;
    10. }

    运行结果:

    3. is_integral    检查类型是否为整型

    1. #include
    2. #include
    3. using namespace std;
    4. class A {};
    5. enum E: int {};
    6. template <class T>
    7. T fn(T i)
    8. {
    9. static_assert (std::is_integral::value, "Integral required.");
    10. return i;
    11. }
    12. int main()
    13. {
    14. cout << "std::is_integral::value======= " << std::is_integral::value << endl;
    15. cout << "std::is_integral::value======= " << std::is_integral::value << endl;
    16. cout << "std::is_integral::value=== " << std::is_integral<float>::value << endl;
    17. cout << "std::is_integral::value===== " << std::is_integral<int>::value << endl;
    18. cout << "std::is_integral::value==== " << std::is_integral<bool>::value << endl;
    19. cout << "fn(123)========================== " << fn(123) << endl;
    20. //cout << "fn(123.5)======================== " << fn(123.5) << endl; //编译失败
    21. cout << "Hello World!" << endl;
    22. return 0;
    23. }

    运行结果:

    4. is_floating_point 检查类型是否是浮点类型

    1. #include
    2. #include
    3. using namespace std;
    4. class A {};
    5. int main()
    6. {
    7. cout << "std::is_floating_point::value====== " << std::is_floating_point::value << endl;
    8. cout << "std::is_floating_point::value== " << std::is_floating_point<float>::value << endl;
    9. cout << "std::is_floating_point::value== " << std::is_floating_point<float&>::value << endl;
    10. cout << "std::is_floating_point::value= " << std::is_floating_point<double>::value << endl;
    11. cout << "std::is_floating_point::value= " << std::is_floating_point<double&>::value << endl;
    12. cout << "std::is_floating_point::value==== " << std::is_floating_point<int>::value << endl;
    13. cout << "Hello World!" << endl;
    14. return 0;
    15. }

    运行结果:

    5. is_array    检查类型是否是数组类型

    1. #include
    2. #include
    3. using namespace std;
    4. class A {};
    5. int main()
    6. {
    7. cout << "std::is_array::value============ " << std::is_array::value << endl;
    8. cout << "std::is_array::value========== " << std::is_array::value << endl;
    9. cout << "std::is_array::value========= " << std::is_array3]>::value << endl;
    10. cout << "std::is_array::value======== " << std::is_array<float>::value << endl;
    11. cout << "std::is_array::value========== " << std::is_array<int>::value << endl;
    12. cout << "std::is_array::value======== " << std::is_array<int[]>::value << endl;
    13. cout << "std::is_array::value======= " << std::is_array<int[3]>::value << endl;
    14. cout << "std::is_array>::value= " << std::is_arrayint,3>>::value << endl;
    15. cout << "Hello World!" << endl;
    16. return 0;
    17. }

    运行结果:

    6. is_enum    检查类型是否是枚举类型

    1. #include
    2. #include
    3. using namespace std;
    4. class A {};
    5. enum E {};
    6. enum class Ec: int {};
    7. int main()
    8. {
    9. cout << "std::is_enum::value====== " << std::is_enum::value << endl;
    10. cout << "std::is_enum::value====== " << std::is_enum::value << endl;
    11. cout << "std::is_enum::value===== " << std::is_enum::value << endl;
    12. cout << "std::is_enum::value==== " << std::is_enum<int>::value << endl;
    13. cout << "Hello World!" << endl;
    14. return 0;
    15. }

    运行结果:

    7. is_union    检查类型是否为联合体类型

    1. #include
    2. #include
    3. using namespace std;
    4. class A {};
    5. typedef union {
    6. int a;
    7. float b;
    8. } B;
    9. class C {
    10. B d;
    11. };
    12. int main()
    13. {
    14. cout << "std::is_union::value======== " << std::is_union::value << endl;
    15. cout << "std::is_union::value======== " << std::is_union::value << endl;
    16. cout << "std::is_union::value======== " << std::is_union::value << endl;
    17. cout << "std::is_union::value====== " << std::is_union<int>::value << endl;
    18. cout << "Hello World!" << endl;
    19. return 0;
    20. }

     运行结果:

    8. is_class    检查类型是否非联合类类型

    1. #include
    2. #include
    3. using namespace std;
    4. struct A { };
    5. class B { } ;
    6. enum class C { };
    7. int main()
    8. {
    9. cout << "std::is_class::value========== " << std::is_class::value << endl;
    10. cout << "std::is_class::value========== " << std::is_class::value << endl;
    11. cout << "std::is_class::value========== " << std::is_class::value << endl;
    12. cout << "std::is_class::value======== " << std::is_class<int>::value << endl;
    13. cout << "Hello World!" << endl;
    14. return 0;
    15. }

    运行结果:

    9. is_function    检查是否为函数类型

    1. #include
    2. #include
    3. using namespace std;
    4. struct A {
    5. int fun() const &;
    6. };
    7. template<typename>
    8. struct PM_traits { };
    9. template<class T, class U>
    10. struct PM_traits {
    11. using member_type = U;
    12. };
    13. int f();
    14. int main()
    15. {
    16. cout << "std::is_function::value================= " << std::is_function::value << endl;
    17. cout << "std::is_function::value========== " << std::is_function<int(int)>::value << endl;
    18. cout << "std::is_function::value======= " << std::is_function<decltype(f)>::value << endl;
    19. cout << "std::is_function::value=============== " << std::is_function<int>::value << endl;
    20. using T = PM_traits<decltype(&A::fun)>::member_type; //T为int() const&
    21. cout << "std::is_function::value================= " << std::is_function::value << endl;
    22. cout << "Hello World!" << endl;
    23. return 0;
    24. }

    运行结果:

    10. is_pointer    检查类型是否为指针类型

    1. #include
    2. #include
    3. using namespace std;
    4. struct A {
    5. int fun() const &;
    6. };
    7. int main()
    8. {
    9. cout << "std::is_pointer::value ================= " << is_pointer::value << endl;
    10. cout << "std::is_pointer::value =============== " << is_pointer::value << endl;
    11. cout << "std::is_pointer::value =============== " << is_pointer::value << endl;
    12. cout << "std::is_pointer::value =============== " << is_pointer<int>::value << endl;
    13. cout << "std::is_pointer::value ============= " << is_pointer<int *>::value << endl;
    14. cout << "std::is_pointer::value ============ " << is_pointer<int **>::value << endl;
    15. cout << "std::is_pointer::value =========== " << is_pointer<int[10]>::value << endl;
    16. cout << "std::is_pointer::value ========= " << is_pointer<nullptr_t>::value << endl;
    17. cout << "std::is_pointer::value = " << is_pointer<decltype(nullptr)>::value << endl;
    18. cout << "Hello World!" << endl;
    19. return 0;
    20. }

    运行结果:

    11. is_lvalue_reference    检查类型是否为左值引用

    1. #include
    2. #include
    3. using namespace std;
    4. class A { };
    5. int main()
    6. {
    7. cout << "std::is_lvalue_reference::value====== " << std::is_lvalue_reference::value << endl;
    8. cout << "std::is_lvalue_reference::value===== " << std::is_lvalue_reference::value << endl;
    9. cout << "std::is_lvalue_reference::value==== " << std::is_lvalue_reference::value << endl;
    10. cout << "std::is_lvalue_reference::value==== " << std::is_lvalue_reference<int>::value << endl;
    11. cout << "std::is_lvalue_reference::value=== " << std::is_lvalue_reference<int&>::value << endl;
    12. cout << "std::is_lvalue_reference::value== " << std::is_lvalue_reference<int&&>::value << endl;
    13. cout << "Hello World!" << endl;
    14. return 0;
    15. }

    运行结果:

    12. is_rvalue_reference    检查类型是否为右值引用

    1. #include
    2. #include
    3. using namespace std;
    4. class A { };
    5. int main()
    6. {
    7. cout << "std::is_rvalue_reference::value====== " << std::is_rvalue_reference::value << endl;
    8. cout << "std::is_rvalue_reference::value===== " << std::is_rvalue_reference::value << endl;
    9. cout << "std::is_rvalue_reference::value==== " << std::is_rvalue_reference::value << endl;
    10. cout << "std::is_rvalue_reference::value==== " << std::is_rvalue_reference<int>::value << endl;
    11. cout << "std::is_rvalue_reference::value=== " << std::is_rvalue_reference<int&>::value << endl;
    12. cout << "std::is_rvalue_reference::value== " << std::is_rvalue_reference<int&&>::value << endl;
    13. cout << "Hello World!" << endl;
    14. return 0;
    15. }

    运行结果:

    13. is_member_object_pointer    检查类型是否为指向非静态成员对象的指针

    1. #include
    2. #include
    3. using namespace std;
    4. int main()
    5. {
    6. class cls {};
    7. std::cout << (std::is_member_object_pointer<int(cls::*)>::value
    8. ? "T is member object pointer"
    9. : "T is not a member object pointer") << endl;
    10. std::cout << (std::is_member_object_pointer<int(cls::*)()>::value
    11. ? "T is member object pointer"
    12. : "T is not a member object pointer") << endl;
    13. cout << "Hello World!" << endl;
    14. return 0;
    15. }

    运行结果:

    14. is_member_function_pointer    检查类型是否为指向非静态成员函数的指针

    1. #include
    2. #include
    3. using namespace std;
    4. class A {
    5. public:
    6. void member() { }
    7. };
    8. int main()
    9. {
    10. // 若 A::member 是数据成员而非函数,则在编译时失败
    11. static_assert(std::is_member_function_pointer<decltype(&A::member)>::value,
    12. "A::member is not a member function.");
    13. cout << "Hello World!" << endl;
    14. return 0;
    15. }

    参考:

    标准库头文件 - cppreference.com

  • 相关阅读:
    tcp三次握手四次挥手
    Nodejs -- 数据库基本概念的介绍及在Express中操作数据库
    Java导出请求头
    郑州中创|第二期星际文件系统训练营
    java Arrays类
    xv6源码阅读——xv6的启动,进程初识
    HashMap为什么会发生死循环?
    关于PointHeadBox类的理解
    自然辩证法与人工智能:一种哲学与技术的对话
    【阅读笔记】对比度增强-《Efficientcontrast enhancement using adaptive gamma correction with weighting distribution 》
  • 原文地址:https://blog.csdn.net/chenyijun/article/details/125822370
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号