Why on earth does the following piece of code work?
struct A {
std::vector<A> subAs;
};
A is an incomplete type, right? If there was a vector of A*s I would understand. But here I don't understand how it works. It seems to be a recursive definition.
This paper was adopted into C++17 which allows incomplete types to be used in certain STL containers. Prior to that, it was Undefined Behavior. To quote from the paper:
And as for the changes in the standard (emphasis mine):
So, there you have it, if you leave the default
std::allocator<T>
in place when instantiating thestd::vector<T, Allocator>
, then it will always work with an incomplete typeT
according to the paper; otherwise, it depends on your Allocator being instantiable with an incomplete typeT
.There is no recursion there. In an extremely simplified form, it's similar to:
Technically, apart from
size
,capacity
and possiblyallocator
,std::vector
only needs to hold a pointer to a dynamic array ofA
it manages via its allocator. (And the size of a pointer is known at compile time.)So, an implementation may look like this: