如何在联合中构建结构?

How do I construct a struct in a union?

下面的t2导致下面的编译错误

test.cpp:14:15: error: cannot initialize a member subobject of type 'E' with an rvalue of type 'int'
        Test t2{2, {{2, 3}}};
                    ^
test.cpp:14:18: error: cannot initialize a member subobject of type 'void *' with an rvalue of type 'int'
        Test t2{2, {{2, 3}}};

如何初始化第二组?如果我切换结构和联合是否可能?

enum E {
    ea, eb, ec
};
struct Test {
    int a;
    union {
        struct{E e; void*p; };
        struct{int b, c; };
    };
};

int main() {
    Test t1{1, {{ea, 0}}};
    Test t2{2, {{2, 3}}};
}

很简单,只有union的第一个(非静态)成员可以用这种方式初始化。阅读更多:

http://www.hellenico.gr/cpp/w/cpp/language/aggregate_initialization.html