Is there a way to initialize an array of objects which don't have a default constructor?
struct IndexAndSign {
const int depth;
const bool baseIsPositive;
IndexAndSign(int depth, bool baseIsPositive)
: depth(depth)
, baseIsPositive(baseIsPositive) {}
};
void test() {
auto arr = new IndexAndSign[size]; // error
}
Yes, it is doable if you are using c++17 or greater. You do this using
std::allocator::allocateto allocate an array and start the lifetime of the array but not the array's objects. The array's objects are then initialized with a placement new in a for loop per OP's comment.