#include <array>
struct A
{
A(char value, char another);
const std::array<char, 42> something; // how to init?
}
Considered approaches:
const_cast
is always dangerous and often UBstd::string
worries me as inefficient and this is an embedded devicestd::experimental::make_array
looks promising but not standard yetCan it be done and how?
Use the member initialization list of A
's constructor to initialize the array, eg:
A::A(char value) : arr{value} {}
UPDATE: you edited your question after I posted this answer.
In your original code, the constructor took a single char
, and the array
held 1 char
, so it was possible to fully initialize the array
with that char
.
Now, in your new code, the constructor takes 2 char
s, but the array
holds 42 char
s, so the only way to fully initialize the array
with any values other than zeros requires using a helper function that returns a filled array
, eg:
std::array<char, 42> makeArr(char value) {
std::array<char, 42> temp;
temp.fill(value);
return temp;
}
A::A(char value, char another) : arr{makeArr(value)} {}