C++ 为什么我不能在最新版本的 gcc 中使用 auto 关键字
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10063884/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
why i cannot use the auto keyword in the last version of gcc
提问by Roger Luo
All, recently i tried to use the new features supported by c++11, and i wrote such statement however the compiler ran failed.
全部,最近我尝试使用c++11支持的新功能,我写了这样的语句但是编译器运行失败。
auto x = 1;
the report error listed below:
报告错误如下:
D:\DEV\CBCppTest\main.cpp||In function 'int main()':|
D:\DEV\CBCppTest\main.cpp|22|warning: 'auto' changes meaning in C++11; please remove it [-Wc++0x-compat]|
D:\DEV\CBCppTest\main.cpp|22|error: 'x' does not name a type|
||=== Build finished: 1 errors, 1 warnings ===|
Why the last gcc version 4.7.0 on MinGW cannot support the this statement. But the compiler of vs10 passed. Could anyone know the reason for this issue?
为什么 MinGW 上的最后一个 gcc 版本 4.7.0 不能支持这个声明。但是vs10的编译器通过了。谁能知道这个问题的原因?
回答by dexametason
"GCC provides experimental support for the 2011 ISO C++ standard. This support can be enabled with the -std=c++11 or -std=gnu++11 compiler options; the former disables GNU extension."
“GCC 为 2011 ISO C++ 标准提供实验性支持。可以使用 -std=c++11 或 -std=gnu++11 编译器选项启用此支持;前者禁用 GNU 扩展。”
It comes from here: c+11 support
它来自这里:c+11 支持
回答by leftaroundabout
To explain what the compiler is actually complaining about: auto
used to be an old C keyword, declaring that this variable has automatic storage. These keywords have little to do with the type system, they specify how variable are represented in memory: where they're stored (processor register vs. main memory / stack) and how the memory is reclaimed. auto
means the variable is stored on the stack (though the processor may optimise it into a processor register) and the memory is automatically reclaimedwhen the variable goes out of scope – which is the right choice in almost any situation1and thus the default, so virtually nobody ever used this old auto
keyword. Yet C++03
still provided backwards compatibility for code that has it; today's compilers still want to support legacy code.
解释编译器实际上在抱怨什么:auto
曾经是一个旧的 C 关键字,声明此变量具有自动存储。这些关键字与类型系统几乎没有关系,它们指定变量在内存中的表示方式:它们的存储位置(处理器寄存器与主内存/堆栈)以及内存如何回收。auto
意味着变量存储在堆栈中(尽管处理器可能会将其优化为处理器寄存器)并且当变量超出范围时会自动回收内存- 这几乎在任何情况下都是正确的选择1因此是默认值,所以几乎没有人使用过这个旧auto
关键字。然而C++03
仍然为拥有它的代码提供向后兼容性;今天的编译器仍然希望支持遗留代码。
1Though often you want objectsto reside on the heap, you'll still be accessing those through variables on the stack; C++ has its own methods of using heap-allocated memory (new
, std::vector
etc.), you don't need the unsafe C-style malloc
stuff.
1虽然您经常希望对象驻留在堆上,但您仍将通过堆栈上的变量访问它们;C ++有自己的使用堆分配的内存(的方法new
,std::vector
等等),你不需要不安全的C风格的malloc
东西。
回答by Johan Kotlinski
When compiling, you need to add -std=c++11
to g++ command line.
编译时需要-std=c++11
在g++命令行中添加。
回答by Raj
This is due to the feature not being enable by default by the GCC compiler. If you're on Codeblocks, go to Settings --> Compiler and enable the feature as shown - http://imgur.com/KrHx8nh
这是因为 GCC 编译器默认未启用该功能。如果您使用的是代码块,请转到设置 --> 编译器并启用如图所示的功能 - http://imgur.com/KrHx8nh
回答by arm
For me adding "-std=c++0x"" to g++ command line fixed the issue.
对我来说,在 g++ 命令行中添加“-std=c++0x”解决了这个问题。