【问题标题】:Compiler error initializing std::array of structs with clang使用 clang 初始化结构的 std::array 时出现编译器错误
【发布时间】:2014-08-14 22:56:28
【问题描述】:

我有一些代码:

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-a-stdarray-of-structs

我想知道这是不正确的 C++11 语法,还是仅仅是 clang 3.4 中的一个缺陷。

是否与Initializing simple structs using initializer lists with clang中提到的bug有关

【问题讨论】:

    标签: c++ c++11 compiler-errors clang stdarray


    【解决方案1】:

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

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

    【讨论】:

    • 啊,这澄清了事情。似乎这是C ++的疏忽。有趣的是,他们已经开始下一个版本了。
    猜你喜欢
    • 2020-01-08
    • 2020-08-19
    • 1970-01-01
    • 2020-12-03
    • 1970-01-01
    • 1970-01-01
    • 2019-10-16
    • 2012-03-08
    • 2019-04-26
    相关资源
    最近更新 更多