How to determine the version of the C ++ standard used by the compiler?

How do you determine which version of the C ++ standard is implemented by your compiler? As far as I know, the following are the standards that I know:

  • C ++ 03
  • C ++ 98
+76
c ++ standards
Feb 24 '10 at 8:46
source share
6 answers

To my knowledge, there is no general way to do this. If you look at the headers of cross-platform / multi-component libraries that support the compiler, you will always find many of the definitions that use compiler-specific constructs to define such things:

/*Define Microsoft Visual C++ .NET (32-bit) compiler */ #if (defined(_M_IX86) && defined(_MSC_VER) && (_MSC_VER >= 1300) ... #endif /*Define Borland 5.0 C++ (16-bit) compiler */ #if defined(__BORLANDC__) && !defined(__WIN32__) ... #endif 

You will probably have to make these definitions yourself for all the compilers you use.

+8
Feb 24 '10 at 9:23
source share

From the Bjarne Stroustrup C ++ 0x FAQ :

__cplusplus

In C ++ 0x, the __cplusplus macro will be set to a value that differs from (more) the current 199711L .

Although it is not as useful as we would like. gcc (apparently for almost 10 years) this value was set to 1 , excluding one main compiler, until it was fixed when gcc 4.7.0 appeared .

These are C ++ standards and what value you should expect in __cplusplus :

  • C ++ pre-C ++ 98: __cplusplus is 1 .
  • C ++ 98: __cplusplus is 199711L .
  • C ++ 98 + TR1: This reads like C ++ 98, and there is no way to check what I know.
  • C ++ 11: __cplusplus is 201103L .
  • C ++ 14: __cplusplus is 201402L .
  • C ++ 17: __cplusplus is 201703L .

If the compiler may be older than gcc , we need to refer to the specific hacking of the compiler (look at the macro version, compare it with the table with the implemented functions) or use Boost.Config (which provides the corresponding macros ). The advantage of this is that we can really choose the specific functions of the new standard and write a workaround if this function is missing. This is often preferable for a bulk solution, as some compilers will require the implementation of C ++ 11, but offer only a set of functions.

The Stdcxx Wiki hosts a comprehensive matrix to support the C ++ 0x function compiler (if you decide to test the functions themselves).

Unfortunately, a finer-grained check of functions (for example, individual library functions, such as std::copy_if ) can only be performed in the build system of your application (run the code using the function, check if it is compiled and get the correct results - autoconf is the tool of choice if you take this route).

+179
Aug 20 '11 at 2:59 a.m.
source share

Please run the following code to check the version.

 #include<iostream> int main() { if (__cplusplus == 201703L) std::cout << "C++17\n"; else if (__cplusplus == 201402L) std::cout << "C++14\n"; else if (__cplusplus == 201103L) std::cout << "C++11\n"; else if (__cplusplus == 199711L) std::cout << "C++98\n"; else std::cout << "pre-standard C++\n"; } 
+7
Jul 26 '18 at 10:16
source share

Depending on what you want to achieve, Boost.Config may help you. It does not provide detection of the standard version, but provides macros that allow you to check support for certain language / compiler functions.

+5
Feb 24 '10 at 9:35
source share

Following a quick google :

__STDC__ and __STDC_VERSION__ , see here

+3
Feb 24 '10 at 8:50
source share

__ cplusplus

In C ++ 0x, the __cplusplus macro will be set to a value that differs from (more than) the current 199711L.

C ++ 0x BS FAQ

+3
Feb 24 '10 at 9:48
source share



All Articles