I have tried to come up (just as an experiment, nothing serious) with a macro that wraps alloca in a more object-oriented way, using special "constructor" that returns the size the instance will need together with a pointer to a function that should initialize it.
Writing that using more statements is simple:
void (*initf)(Type *inst);
Type *obj = alloca(Type::init(&initf, ...));
initf(obj);
Obviously, wrapping that in a function would work if it was something else than alloca, but this needs to be inline.
Is it possible to make an expression that performs this kind of task, returning obj
as its result? The design doesn't have to stay the same, but generally, there should be a function taking the arguments a constructor should take and produce the size and initializer function. If alloca took as a parameter std::pair<size_t,void(*)(void *ptr)>
, it would all be much easier.
I suspect it may not be achieavable in C++, but just for curiosity, could it be done in C? It seems many features intended for macros were added lately.