以下是使用 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
,结果是一样的。 - Mine