简体   繁体   English

警告:“auto”类型说明符是 C++11 扩展

[英]warning: 'auto' type specifier is a C++11 extension

I have a very simple C++ code statement auto a = 12;我有一个非常简单的 C++ 代码语句auto a = 12; . .

When I am compiling it with g++ in Linux using -std=c++98 option I am getting an error as expected当我使用-std=c++98选项在 Linux 中使用 g++ 编译它时,我收到了预期的错误

error: 'a' does not name a type

But when I am compiling the same code with the same option in MacOS I am getting just a warning, but the code get's compiled fine.但是当我在 MacOS 中使用相同的选项编译相同的代码时,我只会收到一个警告,但代码编译得很好。

warning: 'auto' type specifier is a C++11 extension [-Wc++11-extensions]

But wasn't the whole point of -std=c++98 to compile the code following C++ 98 standards?但是-std=c++98的全部意义不在于按照 C++ 98 标准编译代码吗? So the warning tells even though auto is a C++11 extension I am going to compile it for you?所以警告告诉即使 auto 是 C++11 扩展我要为你编译它?

Is there any option to force using c++98 (or other standard)?是否有任何选项可以强制使用c++98 (或其他标准)?

g++ --version prints Apple clang version 12.0.0 (clang-1200.0.32.29) which is another weird thing by the way. g++ --version打印Apple clang version 12.0.0 (clang-1200.0.32.29)顺便说一句,这是另一个奇怪的事情。 So it is clang actually.所以实际上是 clang。

It's like asking firefox --version and getting chrome 87.0.4280.163这就像问firefox --version并获得chrome 87.0.4280.163

On MacOS, you're using clang, not gcc.在 MacOS 上,您使用的是 clang,而不是 gcc。 (If I recall correctly, MacOS provides "gcc" and "g++" as symlinks to "clang" and "clang++", respectively, so that scripts that assume gcc don't break.) (如果我没记错的话,MacOS 分别提供了“gcc”和“g++”作为“clang”和“clang++”的符号链接,因此假设 gcc 的脚本不会中断。)

The two compilers just treat this case differently.这两个编译器只是以不同的方式处理这种情况。

Yes, a compiler that conforms to the 1998 ISO C standard, as both gcc and clang attempt to do with -std=c++98 , must diagnose that line.是的,符合 1998 ISO C 标准的编译器,因为 gcc 和 clang 尝试使用-std=c++98来诊断该行。

As far as the standard is concerned, a non-fatal warning is a valid diagnostic.就标准而言,非致命警告是有效的诊断。 The standard doesn't require an invalid program to be rejected (unless it contains a #error directive).该标准不要求拒绝无效程序(除非它包含#error指令)。

If you want to strictly enforce C++98 rules and reject code that violates them, use -std=c++98 -pedantic-errors (with either gcc or clang).如果您想严格执行 C++98 规则并拒绝违反它们的代码,请使用-std=c++98 -pedantic-errors (使用 gcc 或 clang)。

You may be looking for -pedantic or -pedantic-errors , quoting man g++ :您可能正在寻找-pedantic-pedantic-errors ,引用man g++

-pedantic -迂腐的

Issue all the warnings demanded by strict ISO C and ISO C ++;发出严格的 ISO C 和 ISO C ++ 要求的所有警告; reject all programs that use forbidden extensions, and some other programs that do not follow ISO C and ISO C ++.拒绝所有使用禁止扩展的程序,以及其他一些不遵循 ISO C 和 ISO C ++ 的程序。 For ISO C, follows the version of the ISO C standard specified by any -std option used.对于 ISO C,遵循由使用的任何 -std 选项指定的 ISO C 标准版本。

Note: this is not meant as a feature to check strict standards conformance (see the rest of the description in the man page)注意:这并不是检查严格标准一致性的功能(请参阅手册页中描述的 rest)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM