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

AnI*_*ind 10 c++ gcc compilation g++ c++11

经常提到-std标志应该用于指定编译C++程序时希望使用的标准(例如,-std=c++11-std=gnu++11).一个通常没有解决的相关问题(至少据我所知;例如,参见Dennis根据Oskar N选择的答案高度评价的评论)是如何确定默认的C++标准是什么正在被编译器使用.

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

在描述中-std,手册页列出了所有C++标准,包括GNU方言.在一个特定的标准下,它是相当不明确的,This is the default for C++ code.(C标准有一个类似的陈述:) 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.

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

编辑:我遇到了这个相关的问题,但那里的答案相当复杂,并没有产生具体的,明确的陈述.也许我应该在得到证实之后将其作为对这个问题的答案.

max*_*x66 18

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

#include <iostream>

int main()
 { std::cout << __cplusplus << std::endl; }
Run Code Online (Sandbox Code Playgroud)

打印的值应该说使用的版本:

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

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


Con*_*mer 5

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
Run Code Online (Sandbox Code Playgroud)

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