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

◇◆丶佛笑我妖孽 提交于 2020-06-24 06:15:27

问题


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.


回答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.




回答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.



来源:https://stackoverflow.com/questions/46980383/how-to-determine-what-c-standard-is-the-default-for-a-c-compiler

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!