Question

I want to have a macro, which can merge the type of a variable with another token to create a new token. A random example:

int var;
int make_token(var);

This would become:

int var;
int var_int;

I've tried to define the macro like so:

#define make_token(x) x ## _ ## typeof(x)

But the whole thing gets expanded to:

int var;
int var_typeof(var)

Is there a way to achieve this without having to pass the type of the variable in the parameters of the macro?

Was it helpful?

Solution

Since typeof is determined in a later phase of compilation, way after preprocessing, this is impossible.

Also typeof does not expand to a type identifier; it does not work at the textual/code level, but at the semantic level instead. It's a keyword the compiler interprets and uses to determine the type for its internal data structures.

Only thing you can do is to add the type to the macro:

#define TYPED_VARIABLE(type, identifier) type identifier ## _ ## type

However, as a wise man recently said: "Using Hungarian notation is brainless and is a huge error, so don't do it!" I fully agree.

OTHER TIPS

You'd need a macro for each type you want to define.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top