initialise a C++ std::array of struct in C++11

不问归期 提交于 2021-01-03 07:08:11

问题


I've read the similar questions, but does anyone know why if I have a struct

struct ArabicRoman {
    char roman;
    int arabic;
};

I can initialise a C++ std::array in the following way:

ArabicRoman M({'M', 1000});
ArabicRoman D({'D', 500});
array<ArabicRoman, 2> const SYMBOLS({ M, D });

I can initialise a C-style array in the following way:

ArabicRoman const SYMBOLS[]({ {'M', 1000}, {'D', 500} });

However, the following is not compiling:

array<ArabicRoman, 2> const SYMBOLS({ {'M', 1000}, {'D', 500} });

any workaround to initialise C++ style arrays of structs?


回答1:


You need to replaces parentheses with braces:

std::array<ArabicRoman, 2> const SYMBOLS {{ {'M', 1000}, {'D', 500} }};
                                         ^                           ^

LIVE DEMO



来源:https://stackoverflow.com/questions/33586315/initialise-a-c-stdarray-of-struct-in-c11

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!