Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 882
AnInquiringMind Avatar asked Oct 27 '17 17:10

AnInquiringMind


People also ask

What is the default C compiler?

A default occurs when a borrower stops making the required payments on a debt. Defaults can occur on secured debt, such as a mortgage loan secured by a house, or unsecured debt such as credit cards or a student loan.

What C standard does GCC default to?

By default, gcc does not conform to any of the ANSI/ISO C standards. The current default is equivalent to -std=gnu90 , which is the 1989/1990 standard with GNU-specific extensions. (Some diagnostics required by the language standard are not issued.)

What is the standard version of C?

C17 is the informal name for ISO/IEC 9899:2018, the most recent standard for the C programming language, prepared in 2017 and published in June 2018. It replaced C11 (standard ISO/IEC 9899:2011).


2 Answers

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.

like image 76
max66 Avatar answered Sep 17 '22 15:09

max66


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.

like image 20
ConsistentProgrammer Avatar answered Sep 20 '22 15:09

ConsistentProgrammer