使用clang初始化std::结构数组时编译器错误

Compiler error initializing std::array of structs with clang

本文关键字:编译器 错误 数组 结构 初始化 std 使用 clang      更新时间:2023-10-16
我有一些代码:
std::array<JNINativeMethod, 26> methods = {
    { "nativeCreate", "(Ljava/lang/String;)J", reinterpret_cast<void*>(&nativeCreate) },
    { "nativeDestroy", "(J)V", reinterpret_cast<void*>(&nativeDestroy) },
    ...
    { "nativeToggleDebug", "(J)V", reinterpret_cast<void*>(&nativeToggleDebug) }}
};

我正在尝试用Android NDKs clang 3.4编译器编译。

但是这个代码给了我这个错误:

jni/JNI.cpp:252:9: error: excess elements in struct initializer
        { "nativeDestroy", "(J)V", reinterpret_cast<void*>(&nativeDestroy) },
        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

除非我再加一组大括号:

std::array<JNINativeMethod, 26> methods = {{
    { "nativeCreate", "(Ljava/lang/String;)J", reinterpret_cast<void*>(&nativeCreate) },
    { "nativeDestroy", "(J)V", reinterpret_cast<void*>(&nativeDestroy) },
    ...
    { "nativeToggleDebug", "(J)V", reinterpret_cast<void*>(&nativeToggleDebug) }}
}};

这对我来说似乎很奇怪,但在发现这个关于Visual c++的讨论之后:http://social.msdn.microsoft.com/forums/vstudio/en us/e5ad8fa5 c9e8 - 4328 a7fa af7a47ce2492/initialising -的- stdarray结构

我想知道这是不正确的c++ 11语法,还是只是clang 3.4的缺陷。

是否与clang

使用初始化器列表初始化简单结构体时提到的错误有关?

std::array是一个包含数组的聚合类;因此,需要两对大括号,一对在类成员初始化项周围,另一对在数组元素初始化项周围。

我相信c++ 14将放宽这一要求,允许从外部初始化器列表初始化嵌套数组元素。