This cannot be done in C++, C++ simply does not work this way on a fundamental level.
In C++ all objects have a defined type, and a defined, fixed size. You are using a common C-like approach of manually over-allocating memory so that there can be additional Entry
s in memory, after the pro-forma one.
Taking specific, strict, well-controlled steps allows this to be done without breaking (most) "the rules" (but only assuming that Entry
is trivial, of course, which is unclear).
However introducing a variant into the mix is pretty much a showstopper. A std::variant
is (certainly) not a trivial POD. This particular showstopper can, potentially, be worked around using placement new after over-allocating a calculated amount of memory for this kind of a franken-variant.
However once past this hurdle the next one is insurmountable: C++ gives you no guarantees, whatsoever, as to a variant's internal layout. After all, a variant must keep track of which variant value is constructed, and the additional metadata for all of that can appear either before or after the reserved space for the value of the variant. It is not specified. Game over.