What can and can't the preprocessor do?
I want the code
to be replaced by the code
Code:
#ifdef my_class_##x
#error My error message
#endif
class my_class_##x
{
...
};
However, this macro does not compile:
Code:
#define MY_MACRO(x) \
#ifdef my_class_##x \
#error My error message \
#endif \
class my_class_##x; \
{ \
... \
};
Is there a way to accomplish something like this?
Re: What can and can't the preprocessor do?
I don't think you can have pre-processor instructions inside macros.
Even if you can, the statement #ifdef my_class_##x does not check whether the class my_class_##x has already been declared, it only checks whether the macro my_class_##x has already been defined.
Although I don't think it is a good idea to use macros in that way, I don't see why you can't just use the following:
Code:
#define MY_MACRO(x) \
class my_class_##x \
{ \
... \
};
If my_class_##x already exists then the compiler will complain anyway.
Re: What can and can't the preprocessor do?
Quote:
Originally Posted by HighCommander4
However, this macro does not compile:
Code:
#define MY_MACRO(x) \
#ifdef my_class_##x \
#error My error message \
#endif \
class my_class_##x; \
{ \
... \
};
I just noticed the ; after the x. That's a bug in itself.
Re: What can and can't the preprocessor do?
You need to do it the other way around (typing this off the top of my head...)
Code:
#if CONDITION
#define MYMACRO(X) RealCode()
#else
#define MYMACRO(X) #error "Contition Not Met"
#endif