Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
335 views
in Technique[技术] by (71.8m points)

c++11 - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...