做网站的皮包公司,企业管理咨询有限公司的经营范围,东莞建工集团企业网站,广西城乡和建设厅网站C20定义了一组预处理器宏#xff0c;用于测试各种语言和库的feature。 Feature Test Mocros(特性测试宏)是C20中引入的一种强大机制#xff0c;用于应对兼容性问题。Feature Test Mocros作为预处理器指令(preprocessor directives)出现#xff0c;它使你能够在编译过程中仔细… C20定义了一组预处理器宏用于测试各种语言和库的feature。 Feature Test Mocros(特性测试宏)是C20中引入的一种强大机制用于应对兼容性问题。Feature Test Mocros作为预处理器指令(preprocessor directives)出现它使你能够在编译过程中仔细检查特定语言或库功能(particular language or library feature)是否获得编译器的支持。这种方式提供了一种查询编译器功能的统一方法从而有助于无缝调整代码库。通过战略性地使用Feature Test Mocros开发人员能够识别所选功能(feature)的可用性。因此这允许根据特定属性的存在与否来有条件地组装代码段。总体结果是在一系列编译器和C标准的不同版本中保留代码功能。 C20引入了一套以_cpp为前缀的预定义宏。利用这些宏作为工具来评估所需功能的存在。将取决于特定功能的代码段封装在#ifdef和#endif预处理器指令中。定义宏时相应的代码块将在编译过程中集成相反如果宏仍未定义则编译时会省略该块。 (1).language features宏是在每个翻译单元(translation unit)中预定义的。当工作草案(working draft)中包含了相应的feature时每个宏都扩展为一个与年份和月份相对应的整数字面值。当一个feature发生重大变化时宏将相应地更新。 (2).library features如果包含头文件version或对应的头文件例如any则会定义对应宏。当工作草案(working draft)中包含了相应的feature时每个宏都扩展为一个与年份和月份相对应的整数字面值。当一个feature发生重大变化时宏将相应地更新。 头文件version (1).此头文件是language support library的一部分。此头文件提供有关标准库的实现相关信息(例如特定于实现的库版本宏)。 (2).定义了很多library feature-test macros在实现该feature时扩展为一个数字。这个数字表示该feature被添加到C标准中的年份和月份。 支持的宏列表https://en.cppreference.com/w/cpp/feature_test 以下为测试代码
int test_feature_test_macros()
{// language features
#ifdef __cpp_constexprstd::cout support constexpr std::endl;
#elsestd::cout Warning: unsupport constexpr std::endl;
#endif#ifdef __cpp_structured_bindingsstd::cout support structured bindings std::endl;
#elsestd::cout Warning: unsupport structured bingdings std::endl;
#endif#ifdef __cpp_constevalstd::cout support consteval std::endl;
#elsestd::cout Warning: unsupport consteval std::endl;
#endif#ifdef __cpp_aggregate_paren_initstd::cout support aggregate paren init std::endl;
#elsestd::cout Warning: unsupport aggregate paren init std::endl;
#endif// library features
#ifdef __cpp_lib_rangesstd::cout ranges library available std::endl;
#elsestd::cout Warning: ranges library unavailable std::endl;
#endif#ifdef __cpp_lib_filesystemstd::cout filesystem library available std::endl;
#elsestd::cout Warning: filesystme library unavailable std::endl;
#endif#ifdef __cpp_lib_anystd::cout any library available std::endl;
#elsestd::cout Warning: any library unavailable std::endl;
#endif#ifdef __cpp_lib_fbcstd::cout fbc library available std::endl;
#elsestd::cout Warning: fbc library unavailable std::endl;
#endifreturn 0;
} 执行结果如下图所示选择不同的C语言标准(C14/C17/C20)输出结果不同 GitHubhttps://github.com/fengbingchun/Messy_Test