• C++20中的Feature Test Mocros


          C++20定义了一组预处理器宏,用于测试各种语言和库的feature。

          Feature Test Mocros(特性测试宏)是C++20中引入的一种强大机制,用于应对兼容性问题。Feature Test Mocros作为预处理器指令(preprocessor directives)出现,它使你能够在编译过程中仔细检查特定语言或库功能(particular language or library feature)是否获得编译器的支持。这种方式提供了一种查询编译器功能的统一方法,从而有助于无缝调整代码库。通过战略性地使用Feature Test Mocros,开发人员能够识别所选功能(feature)的可用性。因此,这允许根据特定属性的存在与否来有条件地组装代码段。总体结果是在一系列编译器和C++标准的不同版本中保留代码功能

          C++20引入了一套以_cpp为前缀的预定义宏。利用这些宏作为工具来评估所需功能的存在。将取决于特定功能的代码段封装在#ifdef和#endif预处理器指令中。定义宏时,相应的代码块将在编译过程中集成;相反,如果宏仍未定义,则编译时会省略该块

          (1).language features:宏是在每个翻译单元(translation unit)中预定义的。当工作草案(working draft)中包含了相应的feature时,每个宏都扩展为一个与年份和月份相对应的整数字面值。当一个feature发生重大变化时,宏将相应地更新。

          (2).library features:如果包含头文件或对应的头文件例如,则会定义对应宏。当工作草案(working draft)中包含了相应的feature时,每个宏都扩展为一个与年份和月份相对应的整数字面值。当一个feature发生重大变化时,宏将相应地更新。

          头文件

          (1).此头文件是language support library的一部分。此头文件提供有关标准库的实现相关信息(例如特定于实现的库版本宏)。

          (2).定义了很多library feature-test macros,在实现该feature时扩展为一个数字。这个数字表示该feature被添加到C++标准中的年份和月份。

          支持的宏列表:https://en.cppreference.com/w/cpp/feature_test 

          以下为测试代码:

    1. int test_feature_test_macros()
    2. {
    3. // language features
    4. #ifdef __cpp_constexpr
    5. std::cout << "support constexpr" << std::endl;
    6. #else
    7. std::cout << "Warning: unsupport constexpr" << std::endl;
    8. #endif
    9. #ifdef __cpp_structured_bindings
    10. std::cout << "support structured bindings" << std::endl;
    11. #else
    12. std::cout << "Warning: unsupport structured bingdings" << std::endl;
    13. #endif
    14. #ifdef __cpp_consteval
    15. std::cout << "support consteval" << std::endl;
    16. #else
    17. std::cout << "Warning: unsupport consteval" << std::endl;
    18. #endif
    19. #ifdef __cpp_aggregate_paren_init
    20. std::cout << "support aggregate paren init" << std::endl;
    21. #else
    22. std::cout << "Warning: unsupport aggregate paren init" << std::endl;
    23. #endif
    24. // library features
    25. #ifdef __cpp_lib_ranges
    26. std::cout << "ranges library available" << std::endl;
    27. #else
    28. std::cout << "Warning: ranges library unavailable" << std::endl;
    29. #endif
    30. #ifdef __cpp_lib_filesystem
    31. std::cout << "filesystem library available" << std::endl;
    32. #else
    33. std::cout << "Warning: filesystme library unavailable" << std::endl;
    34. #endif
    35. #ifdef __cpp_lib_any
    36. std::cout << "any library available" << std::endl;
    37. #else
    38. std::cout << "Warning: any library unavailable" << std::endl;
    39. #endif
    40. #ifdef __cpp_lib_fbc
    41. std::cout << "fbc library available" << std::endl;
    42. #else
    43. std::cout << "Warning: fbc library unavailable" << std::endl;
    44. #endif
    45. return 0;
    46. }

          执行结果如下图所示:选择不同的C++语言标准(C++14/C++17/C++20),输出结果不同

          GitHubhttps://github.com/fengbingchun/Messy_Test

  • 相关阅读:
    tensorflow 1.15 gpu docker环境搭建;Nvidia Docker容器基于TensorFlow1.15测试GPU;——全流程应用指南
    出现 CUDA out of memory 的解决方法
    MQTT透传和MQTT网关的区别
    类的成员之一:代码块
    结合SkeyeVSS+SkeyeIVMS运维管控技术构建机关单位视频监控系统
    HummerRisk 使用场景:混合云安全治理(1)简介
    前端架构的艺术:解决问题、优化体验和提升效率
    【Hyper-V】Windows的Hyper-V管理器创建的虚拟机上怎么复制粘贴文件
    jmeter理论
    C++算法之旅、04 基础篇 | 第一章 基础算法
  • 原文地址:https://blog.csdn.net/fengbingchun/article/details/139896405