Click to See Complete Forum and Search --> : What can and can't the preprocessor do?
HighCommander4
August 10th, 2007, 07:02 AM
I want the code
MY_MACRO(x)
to be replaced by the code
#ifdef my_class_##x
#error My error message
#endif
class my_class_##x
{
...
};
However, this macro does not compile:
#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?
Zaccheus
August 10th, 2007, 07:05 AM
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:
#define MY_MACRO(x) \
class my_class_##x \
{ \
... \
};
If my_class_##x already exists then the compiler will complain anyway.
Zaccheus
August 10th, 2007, 07:15 AM
However, this macro does not compile:
#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.
TheCPUWizard
August 10th, 2007, 08:25 AM
You need to do it the other way around (typing this off the top of my head...)
#if CONDITION
#define MYMACRO(X) RealCode()
#else
#define MYMACRO(X) #error "Contition Not Met"
#endif
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.