相关疑难解决方法(0)

在for循环中声明匿名结构,clang无法编译

在for循环中声明匿名结构的代码在gcc中使用-std = c99/gnu99正常工作

for (struct {int foo; int bar;} i = {0}; i.foo < 10; i.foo++);
Run Code Online (Sandbox Code Playgroud)

但是,当我切换到clang而不是我得到错误:

error: declaration of non-local variable in 'for' loop
Run Code Online (Sandbox Code Playgroud)

为什么这是一个错误?为什么它会允许某些类型(例如"int")而不允许其他类型(例如struct {int foo;})?这似乎不一致.clang是否无法正确实现c99或者代码无效c99和gcc恰好支持它?

有没有人知道在clang支持的for循环中声明多种类型变量的方法?(这对宏很有用.)

编辑:

由于人们问为什么这有用,我会粘贴一些示例代码:

#define TREE_EACH(head, node, field, iterator) for ( \
    /* initialize */ \
    struct { \
        node* cur; \
        node* stack[((head)->th_root == 0? 0: (head)->th_root->field.avl_height) + 1]; \
        uint32_t stack_size; \
    } iterator = {.cur = (head)->th_root, .stack_size = 0}; \
    /* while */ \
    iterator.cur != 0; \
    /* …
Run Code Online (Sandbox Code Playgroud)

c c99

8
推荐指数
2
解决办法
1192
查看次数

标签 统计

c ×1

c99 ×1