如何确定 C++ 编译器的默认 C++ 标准是什么?

2022-01-04 00:00:00 gcc compilation g++ c++ c++11

经常提到应该使用-std标志来指定编译C++程序时希望使用的标准(例如,-std=c++11-std=gnu++11).通常未解决的相关问题(至少据我所知;例如,请参见 theDennis 在 Oskar N 所选答案下的高度赞成的评论.) 是如何确定编译器使用的默认 C++ 标准是什么.

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.

我相信可以通过查看手册页(至少对于 g++)来判断,但我想问这是否正确,以及是否有更明确/具体的方法:

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:

-std 的描述下,手册页列出了所有 C++ 标准,包括 GNU 方言.在一个特定的标准下,它是相当不起眼的声明,这是C++代码的默认值.(C标准有一个类似的声明:这是C代码的默认值.).

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.).

例如,对于 g++/gcc version 5.4.0,它列在 gnu++98/gnu++03 下,而对于 g++/gcc version 6.4.0,在gnu++14下列出.

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.

这自然似乎表明默认标准,但它写得所以不显眼,我并不完全确定.如果是这种情况,也许这对那些想知道同样问题的其他人有用.其他C++编译器还有其他方便的方法吗?

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?

我遇到了这个相关问题,但那里的答案相当复杂,并没有产生具体、明确的陈述.或许我应该在得到证实后提交这个作为该问题的答案.

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.

推荐答案

如何编译和执行下面的琐碎程序?

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 用于 C++98,
  • 201103 用于 C++11
  • 201402 用于 C++14
  • 201703 用于 C++17

如果您编译时省略了 -std=c++xx 标志,您应该能够检测到所使用语言的默认版本.

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

相关文章