It is possible, but rather low-level.
James adequately covered the C++03 do it yourself code, so let me present alternatives.
First, in C++03 you have two alternatives with Boost:
- Boost.Optional:
boost::optional<MyClass>
which as the name implies allows you to have (or not) an instance of the type.
- Boost.Variant:
boost::variant<SomeClass,OtherClass,YetAnotherClass>
which is a kind of union
with type safe semantics and guaranteed constructors/destructors calls.
Second, in C++11 was introduced std::aligned_storage<Len,Align>
which takes two parameters:
- the number of bytes of storage required
- the alignment required
It uses dark compiler magic to guarantee that the alignment is satisfied, so it is a good basis for building raw-memory manipulation routines.
Personally I would opt for boost::optional
in your case:
boost::optional<MyClass> Arena[256];
this models exactly what you want because by default boost::optional
is instantiated empty.