I am trying to create a C macro, that given a typename will append _info to it, take the address of that and a call a function with it. Example code (doesn't work):
#define new(X) __new(&(X_info))
struct classInfo
{
size_t size;
void* (*constructor)(void* p);
};
void* __new(struct classInfo* info, ...)
{
return info->constructor(malloc(info->size));
}
void* queue_constructor(void* p)
{
return p;
}
typedef struct
{
uint64_t data;
} queue_t;
const struct classInfo queue_t_info = { .size = sizeof(queue_t),
.constructor = &queue_constructor};
int main(int argc, char** argv)
{
queue_t* p = new(queue_t);
return 0;
}
The preprocessor doesn't seem to want to expand X, as it's error'ing about an undefined symbol X_info. Not sure what I should change on the macro to fix this.
new), as people coming from C++ background might find it confusing, and also some compilers really don't have a proper pure C mode (like MS Visual C++). - Some programmer dude