【发布时间】:2019-10-19 02:14:33
【问题描述】:
当 this code 与 -Warray-bounds 一起编译时。声明 array2 array index 3 is past the end of the array (which contains 3 elements) 时收到警告。但在声明 array1 时不是这样,即使它必须是相同的类型,因此携带相同的大小信息。这是clang中的错误吗?
enum class Format : int {
Off = 55,
FormatA = 66,
FormatB = 77,
};
inline Format (&AllFormats())[3] {
static Format values[] = {
Format::Off,
Format::FormatA,
Format::FormatB
};
return values;
}
int main()
{
auto array1 = AllFormats();
auto v3 = array1[3];
Format (&array2)[3] = AllFormats();
v3 = array2[3];
}
【问题讨论】:
-
您如何验证它们具有相同的类型?您可能需要重新检查。
-
使用
std::array<Format, 3>代替 C 样式的数组。你会发现代码更容易写,也更容易理解。 -
@pete 你是对的。在我的代码中,我会使用 std::array。显示的代码大致由 google flatbuffers 编译器生成。