I would like to define an member which is an array of a type which can not have a default constructor. The array must contain the actual objects, not pointers. Dynamic memory allocation (new/delete) can not be used.
Apparently this is not trivially possible in C++98, as initializer lists are not available yet? What are the alternatives?
Here a MVP:
struct I {
const bool b;
};
struct O {
I a[2];
O() : a{true, false} {}
} o;
Error message with GCC:
<source>: In constructor 'O::O()':
<source>:8:12: error: extended initializer lists only available with '-std=c++11' or '-std=gnu++11' [-Werror=c++11-extensions]
8 | O() : a{true, false} {}
| ^
Using the pointer and answer from @user12002570 combined with the answer from @Cheers and hth. - Alf I designed the following solution:
In this example the reference to a
int[4]needs to be initialized when constructingI(in contrast toconst boolin the question).It wraps the member array to be initialized into a
struct"Container". This container can be initialized within the constructor initialization list of the outer classOusing a function (generateArray()). In this case the generator function takes a reference to a multi-dimensional array as argument.This complies with clang and gcc.
In addition to previous answers this solution uses an initializer with automatic storage duration.