简体   繁体   中英

initialise a C++ std::array of struct in C++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?

You need to replaces parentheses with braces:

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

LIVE DEMO

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM