C++ 判断标准版本和编译器

时间:2020-09-18 01:47:15   收藏:0   阅读:102

 

A、判断c++版本

1、msvc 

  VS 编译器判断 _MSVC_LANG _MSVC_LANG的值如下:

  _MSVC_LANG值
C++17 201703L
C++14 201402L
C++11 201103L  
C++03及低于c++03 199711L

 

 

 

 

 

 

2、gcc / clang 

  gcc/clang需要判断 __cplusplus __cplusplus

  __cplusplus值
C++17 201703L
C++14 201402L
C++11 201103L
C++03及低于03版本 199711L

 

 

 

 

 

 

B、判断编译器

  常见c++编译器有3中: msvcclanggcc, 各自对应的宏分别如下:

msvc _MSC_VER
gcc __GNUC__
clang __clang__

 

 

 

 

C、一个例子

  1、判断c++版本

 1 #if __cplusplus >= 199711L
 2 
 3     #define cpp_03 
 4     
 5 #elif __cplusplus >= 201103L
 6 
 7     #define cpp_11 
 8     
 9 #elif __cplusplus >= 201402L
10 
11     #define cpp_14 
12     
13 #elif __cplusplus >= 201703L
14 
15     #defined cpp_17 
16     
17 #endif

  2、判断编译器

1 #if defined(__clang__) || defined(__GNUC__)
2 
3     #define cpp_standard __cplusplus
4     
5 #elif defined(_MSC_VER)
6 
7     #define cpp_standard _MSVC_LANG
8     
9 #endif

 

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!