C++ judge standard version and compiler

C++ judge standard version and compiler

Note: I am original, if you find similarities, you will be responsible for the consequences

Development environment

Most compilers support

principle

C++ 20 Not sure about
clang and gcc judgment__cplusplus

__cplusplusValue of
C++ 17 201703L
C++ 14 201402L
C++ 11 201103L
Below C++ 03 199711L

msvc judgment_MSVC_LANG

_MSVC_LANGValue of
C++ 17 201703L
C++ 14 201402L
C++ 11 201103L
Below C++ 03 199711L

Determine the compiler used

Macro
msvc _MSC_VER
clang __clang__
gcc __GNUC__

achieve

// cpp.hpp
#ifndef CPP_HPP
#define CPP_HPP

#if defined(__clang__) || defined(__GNUC__)

	#define CPP_STANDARD __cplusplus
	
#elif defined(_MSC_VER)

	#define CPP_STANDARD _MSVC_LANG
	
#endif
 
#if CPP_STANDARD >= 199711L

	#define HAS_CPP_03 true
	
#elif CPP_STANDARD >= 201103L

	#define HAS_CPP_11 true
	
#elif CPP_STANDARD >= 201402L

	#define HAS_CPP_14 true
	
#elif CPP_STANDARD >= 201703L

	#defined HAS_CPP_17 true
	
#endif

#endif

in conclusion

In order to optimize the code and compatibility, you can determine the C++ version used

Guess you like

Origin blog.csdn.net/m0_47534090/article/details/108591764