惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Engineering at Meta
Engineering at Meta
Microsoft Azure Blog
Microsoft Azure Blog
I
InfoQ
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
人人都是产品经理
人人都是产品经理
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
T
Tailwind CSS Blog
MongoDB | Blog
MongoDB | Blog
Google DeepMind News
Google DeepMind News
WordPress大学
WordPress大学
量子位
美团技术团队
大猫的无限游戏
大猫的无限游戏
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Last Week in AI
Last Week in AI
博客园 - 司徒正美
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
小众软件
小众软件
C
Check Point Blog
博客园 - 三生石上(FineUI控件)
N
Netflix TechBlog - Medium
Recent Announcements
Recent Announcements
有赞技术团队
有赞技术团队
月光博客
月光博客

博客园 - bodong

Unreal Engine 5创建项目失败:文件“{ConfigFile}”处理失败。 如何找到当前计算机所有的UnrealEngine安装位置 Unreal Engine 5调试资源依赖来源的方法 GhostDoc Enterprise.v2024.2.24300 for Visual Studio 2022插件漏洞分析和离线解锁方法 ScyllaHide插件可能导致x64dbg的日志窗口无法捕获应用程序输出的日志字符串 VisualStudio各版本_MSC_VER和_MSC_FULL_VER宏定义值列表 Rider如何针对一个项目禁用某一类错误检查(Inspections)的办法 UnrealEngine 5 C++标准版本 UnrealEngine Setup即使挂了代理也很慢的解决办法 CPPStringFormatting: 使用.NET格式化字符串风格格式化C++字符串的库 Windows平台下使用C++和Windows API计算文件MD5的方法 Windows平台下使用C++检测硬盘分区是否属于SSD硬盘的方法 将C++代码文件路径、行号、函数名称等打包到#pragma message输出的方法 KnownDLLs列表 Avalonia实现Visual Studio风格标题栏的方法 解决IDA Pro Warning:mssdk64_win7: No such file or directory AHeadLib.Net C#版本的DLL劫持代码生成工具 安装Visual Studio 2022指定版本的方法 UnrealSharp: 一个可以让你在UnrealEngine5中采用C# 12和.NET 8.0开发的插件
C++使用宏来判断当前编译期支持的C++标准版本的方法
bodong · 2024-07-10 · via 博客园 - bodong

     需要注意的是,很多时候,某个编译器的版本并不完整支持某个C++标准,比如Visual Studio 2010 SP1,虽然支持了部分C++ 11的能力,但是依然有很多C++ 11的特性是不支持的。因此单纯通过C++标准的版本号来鉴别C++特性是否可用是并不完备的方法。具体支持情况可以参考这里

#ifdef _MSC_VER
#define FL_COMPILER_MSVC      1
#else
#define FL_COMPILER_MSVC      0
#endif

#ifdef __GNUC__
#define FL_COMPILER_GCC       1
#else
#define FL_COMPILER_GCC       0
#endif // __GNUC__  // NOLINT

#if FL_COMPILER_MSVC
// @reference https://www.cnblogs.com/bodong/p/18293350
// Each version of Visual Studio does not have complete support for the C++ standard,
// so simply judging the feature support of the C++ standard through these version numbers is incomplete.
#if _MSC_FULL_VER <= 150030729 // before Visual Studio 2008 sp1, set C++ 98
#define _MSVC_LANG 199711
#elif _MSC_FULL_VER <= 180021114 // before Visual Studio 2013 Nobemver CTP, set C++ 11
#define _MSVC_LANG 201103
#elif _MSC_FULL_VER <= 190023918 // before Visual Studio 2015 Update 2, set C++ 14
#define _MSVC_LANG 201402
#endif // after Visual Studio 2015 Update 3, _MSVC_LANG exists

#define FL_COMPILER_LANG_VERSION _MSVC_LANG
#elif defined(__cplusplus)
#define FL_COMPILER_LANG_VERSION __cplusplus
#else // set C++ 98 as default
#define FL_COMPILER_LANG_VERSION 199711
#pragma message("No valid C++ standard identification flag found, default to C++98 standard")
#endif

// is greater than ?
// Checks whether the current C++ standard is a later version
#define FL_COMPILER_IS_GREATER_THAN_CXX23 (FL_COMPILER_LANG_VERSION >= 202101)
#define FL_COMPILER_IS_GREATER_THAN_CXX20 (FL_COMPILER_LANG_VERSION >= 202002)
#define FL_COMPILER_IS_GREATER_THAN_CXX17 (FL_COMPILER_LANG_VERSION >= 201703)
#define FL_COMPILER_IS_GREATER_THAN_CXX14 (FL_COMPILER_LANG_VERSION >= 201402)
#define FL_COMPILER_IS_GREATER_THAN_CXX11 (FL_COMPILER_LANG_VERSION >= 201103)
#define FL_COMPILER_IS_GREATER_TAHN_CXX98 (FL_COMPILER_LANG_VERSION >= 199711)

// is C++ xx ?
// Check whether the current C++ standard specifies a certain version
#define FL_COMPILER_IS_CXX23 (FL_COMPILER_LANG_VERSION >= 202101)
#define FL_COMPILER_IS_CXX20 (FL_COMPILER_LANG_VERSION >= 202002 && FL_COMPILER_LANG_VERSION < 202101)
#define FL_COMPILER_IS_CXX17 (FL_COMPILER_LANG_VERSION >= 201703 && FL_COMPILER_LANG_VERSION < 202002)
#define FL_COMPILER_IS_CXX14 (FL_COMPILER_LANG_VERSION >= 201402 && FL_COMPILER_LANG_VERSION < 201703)
#define FL_COMPILER_IS_CXX11 (FL_COMPILER_LANG_VERSION >= 201103 && FL_COMPILER_LANG_VERSION < 201402)
#define FL_COMPILER_IS_CXX98 (FL_COMPILER_LANG_VERSION >= 199711 && FL_COMPILER_LANG_VERSION < 201103)

       可以简单测试一下:

#if FL_COMPILER_IS_CXX23
#pragma message("C++ 23")
#elif FL_COMPILER_IS_CXX20
#pragma message("C++ 20")
#elif FL_COMPILER_IS_CXX17
#pragma message("C++ 17")
#elif FL_COMPILER_IS_CXX14
#pragma message("C++ 14")
#elif FL_COMPILER_IS_CXX11
#pragma message("C++ 11")
#elif FL_COMPILER_IS_CXX98
#pragma message("C++ 98")
#else
#error "can't find C++ compiler version."
#endif