I have a variable length data structure, a multi-dimensional iterator:
class Iterator
{
public:
    static Iterator& init(int dim, int* sizes, void* mem)
    {
        return *(new (mem) Iterator(dim, sizes));
    }
    static size_t alloc_size(int dim)
    {
        return sizeof(Iterator) + sizeof(int) * 2 * dim;
    }
    void operator++()
    {
        // increment counters, update pos_ and done_
    }
    bool done() const { return done_; }
    bool pos()  const { return pos_; }
private:
    Iterator(int dim, int* sizes) : dim_(dim), pos_(0), done_(false)
    {
        for (int i=0; i<dim_; ++i) size(i) = sizes[i];
        for (int i=0; i<dim_; ++i) counter(i) = 0;
    }
    int  dim_;
    int  pos_;
    bool done_;
    int  size   (int i) { return reinterpret_cast<int*>(this+1)[i]; }
    int& counter(int i) { return reinterpret_cast<int*>(this+1)[dim_+i]; }
};
The dimensionality of the iterator is not known at compile time but probably small, so I allocate memory for the iterator with alloca:
void* mem = alloca(Iterator::alloc_size(dim));
for (Iterator& i = Iterator::create(dim, sizes, mem); !i.done(); ++i)
{
    // do something with i.pos()
}
Is there a more elegant way of allocating memory for the iterator? I am aware of the fact that upon returning from a function, its stack is unwound, thus alloca must be used in the caller's stack frame (see e.g. here). This answer suggests that the allocation be performed in a default parameter:
static Iterator& init(int dim, int* sizes, void* mem = alloca(alloc_size(dim)));
However elegant, this solution does not help me: Default argument references parameter 'dim'. Any suggestion for a nice solution?
 
     
     
     
     
    