Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 546
stivlo Avatar asked Feb 08 '23 15:02

stivlo


1 Answers

You need to replaces parentheses with braces:

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

LIVE DEMO

like image 94
101010 Avatar answered Feb 16 '23 04:02

101010