How to determine what C++ standard is the default for a C++ compiler?

16,739

Solution 1

What about compiling and executing the following trivial program ?

#include <iostream>

int main()
 { std::cout << __cplusplus << std::endl; }

The value printed should say the version used:

  • 199711 for C++98,
  • 201103 for C++11
  • 201402 for C++14
  • 201703 for C++17

If you compile omitting the -std=c++xx flag, you should be able to detect the default version of language used.

Solution 2

Add to max66's answer. There is no need to compile and execute the program. The same information can be grepped through the preprocessed output using:

 g++ -x c++  -E -dM -< /dev/null | grep __cplusplus

The values of the __cplusplus macro gives the value of the standard.

Share:
16,739
AnInquiringMind
Author by

AnInquiringMind

Updated on June 02, 2022

Comments

  • AnInquiringMind
    AnInquiringMind almost 2 years

    It is frequently mentioned that the -std flag should be used to specify the standard that one wishes to use when compiling a C++ program (e.g., -std=c++11 or -std=gnu++11). A related question that is not typically addressed (at least as far as I can tell; see, for instance, the highly-upvoted comment by Dennis under the selected answer by Oskar N.) is how to determine what the default C++ standard that is being used by the compiler is.

    I believe that it is possible to tell by looking at the man page (at least for g++), but I wanted to ask if this is correct and also if there are more definitive/concrete methods:

    Under the description of -std, the man page lists all C++ standards, including the GNU dialects. Under one specific standard, it is rather inconspicuously stated, This is the default for C++ code. (there is an analogous statement for C standards: This is the default for C code.).

    For instance, for g++/gcc version 5.4.0, this is listed under gnu++98/gnu++03, whereas for g++/gcc version 6.4.0, this is listed under gnu++14.

    This would naturally seem to indicate the default standard, but it is written so inconspicuously that I am not entirely certain. If this is the case, perhaps this will be of use to others who have wondered about this very same question. Are there other convenient methods for other C++ compilers?

    Edit: I came across this related question, but the answers there were quite convoluted and did not yield concrete, definitive statements. Perhaps I should submit this as an answer to that question once it has been corroborated.