If an object does not have a default constructor what could it mean to have a block of memory holding one that has not been initialized?  How would your program know if the object had been properly initialized or not?

There are three ways to address this:

1 - A pointer, as already mentioned.  A null pointer means the spot does not refer to ("contain") a valid object -- or any object at all.

2 - use a variant

3 - create a default constructor (which you could do by changing the signature of your constructor to something like (int = 0) or (int = -1) assuming those values are invalid).

In all cases, regardless of semantics or implementation you'll either have to check at runtime for a valid object OR, if you know for other reasons which locations are valid, use the default constructor with any value you'd like.

If you don't want to use pointers there are a couple of other possibilities:


Date: Sun, 22 Nov 2020 12:06:04 +0100
From: Eugenio Bargiacchi <svalorzen@gmail.com>

Ah, sorry, I thought it would have been automatically clear. Consider this
example:

class A {
   public:
       A(int) {}
};

boost::multi_array<A,2> array(boost::extents[2][2]); // Compiler error
boost::multi_array<A,2> array(boost::extents[2][2], A(3)); // What I would
like

array.resize(boost::extents[2][2]); // Again, compiler error
array.resize(boost::extents[2][2], A(2)); // What I would like

The variable `array` cannot be constructed in any way, as multi_array will
try to default-construct it, and that will fail. Trying to simply create
`array` with its default constructor works (although it will obviously be a
zero-sized array), but when trying to resize it (to actually store things),
the same thing will happen.