|
-
August 10th, 2007, 07:02 AM
#1
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?
Old Unix programmers never die, they just mv to /dev/null
-
August 10th, 2007, 07:05 AM
#2
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.
Last edited by Zaccheus; August 10th, 2007 at 07:15 AM.
-
August 10th, 2007, 07:15 AM
#3
Re: What can and can't the preprocessor do?
 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.
-
August 10th, 2007, 08:25 AM
#4
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
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions 
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|