Vector Constructor Iterator

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

inline void __stdcall __vec_ctor (
    void *__t,
    unsigned __s,
    int __n,
    void * (__thiscall *__f) (void *))
    while (-- __n >= 0) {
        (*__f) (__t);
        __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 following fragment induces the compiler to generate a vector constructor iterator:

struct Test
    Test (void);

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

A structure, named Test, is defined with 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: the compiler does it.