c++ - 匿名结构内部循环

标签 c++ gcc g++

下面的代码可以用 g++ 编译好

#include <iostream>
using namespace std; 

int main() 
{
    for (struct { int i; double j; } x = {0,0};  x.i < 10; ++x.i, x.j+=.1)
    {
        std::cout << x.i << " " << x.j << '\n';
    }
}

但是使用 MSVC2005 我会出错

error C2332: 'struct' : missing tag name
error C2143: syntax error : missing ')' before '{'
warning C4094: untagged 'struct' declared no symbols
error C2059: syntax error : 'empty declaration'
error C2143: syntax error : missing ';' before ')'
error C2143: syntax error : missing ';' before ')'
error C2065: 'x' : undeclared identifier
error C2059: syntax error : '{'
error C2143: syntax error : missing ';' before '{'
error C2143: syntax error : missing ';' before '}'
error C2228: left of '.i' must have class/struct/union
1>        type is ''unknown-type''
error C2228: left of '.i' must have class/struct/union
1>        type is ''unknown-type''
error C2228: left of '.j' must have class/struct/union
1>        type is ''unknown-type''
error C2059: syntax error : ')'
error C2143: syntax error : missing ';' before '{'
error C2228: left of '.i' must have class/struct/union
1>        type is ''unknown-type''
error C2228: left of '.j' must have class/struct/union
1>        type is ''unknown-type''

我想知道循环内的匿名结构是“扩展”还是语言功能,而 MSC2005 缺少它?

最佳答案

这是一个bug in msvc .不幸的是,它在他们的优先级列表中并不高。

关于c++ - 匿名结构内部循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15795532/

相关文章:

c++ - 在没有 AVX 的情况下编译 boost

c++ - 将 GNU C++ 程序移植到 Visual C++

c++ - cocos2d 中的 setScale 特定时间 Xcode

c++ - 如何在 Skylake 架构上最大化 sqrt-heavy-loop 的指令级并行性?

c++ - 如何降低以下代码块的时间复杂度?

c++ - 为什么不能存储基类的函数指针?

C For 循环比较使用函数不等待

c - 行尾(GNU 文档)

c++ - 如何避免 PANTHEIOS_FE_PROCESS_IDENTITY 的多重定义?

c++ - GCC:我什么时候可以期望返回大型 C++ 对象是高效的?