If a class has only one constructor with one parameter, how to declare an array? I know that vector is recommended in this case. For example, if I have a class
The proper way is use to std::aligned_storage. You will have to manually construct and destruct items as well as reintrepret_cast when you want to access an item. I recommend you write a small wrapper class around storage_t to take care of this. Someone mentioned using boost::optional which uses a bool and a storage_t under the hood. This method saves you a bool.
template
using storage_t = typename std::aligned_storage::type;
struct Foo;
size_t i = 55;
storage_t items[1000]; // array of suitable storage for 1000 T's
new (reintrepret_cast(items + i)) Foo(42); // construct new Foo using placement new
*reintrepret_cast(items + i) = Foo(27); // assign Foo
reintrepret_cast(items + i)->~Foo() // call destructor