Vector Vbase Constructor Iterator

The so-called “vector vbase constructor iterator” is known internally by the identifier __vec_ctor_vb (at global scope), with the following compiler-generated code:

inline void __stdcall __vec_ctor_vb (
    void *__t,
    unsigned __s,
    int __n,
    void * (__thiscall *__f) (void *))
{
    while (-- __n >= 0) {
        (* (void (__thiscall *) (void *, int)) __f) (__t, 1);
        __t = (char *) __t + __s;
    }
}

Given __n objects each of size __s in an array at address __t, and a default constructor at address __f, the iterator calls the constructor once for each object in the array, in ascending order.

The type cast of __f to accommodate the hidden argument is unsafe, at least in the sense of triggering warning C4191. However, this warning is ordinarily not shown, being one that the compiler initialises as disabled (unless /Wall is given). When the warning is configured to show, the diagnostic note that follows it is one of the few instances of the compiler revealing the name of a compiler-generated function.

Example

The following fragment induces the compiler to generate a vector constructor iterator:

struct Base
{
};

struct Test : virtual Base
{
    Test (void);
};

void *test (int n)
{
    return new Test [n];
}

A structure, named Test, is defined with both a virtual base and a default constructor, which may be coded elsewhere. A function, named test, exists solely to obtain an arbitrary number of Test structures as an array. Once sufficient memory is found for the desired array, successive elements of the array are each constructed by calling the default constructor. The user is not troubled with writing code for the iteration, nor with knowing the details of dealing with the virtual base: the compiler does it all.