【发布时间】:2020-06-02 00:59:03
【问题描述】:
下面给出使用g++ -c test.cpp 或g++ -std=c++17 -c test.cpp 编译的简化代码
#include <cstddef>
struct sd_bus_vtable {
union {
struct {
size_t element_size;
} start;
struct {
const char *member;
const char *signature;
} signal;
} x;
};
sd_bus_vtable get()
{
return {
.x = {
.signal = {
.member = "",
.signature= "",
}
}
};
}
它在 GCC 9.2.0 和 clang 5/6 上编译良好,但在 8.3.0 或 7.4.0 上编译失败并显示以下错误消息:
test.cpp:25:5: error: could not convert ‘{{{"", ""}}}’ from ‘<brace-enclosed initializer list>’ to ‘sd_bus_vtable’
};
要解决这个问题,可以将函数get() 更改如下,但它看起来不太干净...
sd_bus_vtable get()
{
struct sd_bus_vtable t = {
.x = {
.signal = {
.member = "",
.signature= "",
}
}
};
return t;
}
问题是,上面的代码是否有效?如果是,是否会触发 GCC 中已在 GCC9 中修复的某些错误?
【问题讨论】:
-
其实我确实用了
-std=c++17,结果是一样的。