Reputation: 12544
I know I will be punished after asking this, still I'd like to do something like this:
#define DEF_CLASS(x) \
#define CLASS x \
#define CONSTRUCTOR CLASS::CLASS \
#define COPY_CONSTRUCTOR(x) CONSTRUCTOR(const CLASS& x)
That is I'd like to have a "#define" function which dynamically defines other "#defines".
I have an IDE which is not capable of refactoring, furthermore I'd like to make constructors, especially copy constructor to stand out due to its special role.
Therefore I aim to program my c++ class definitions like this:
CONSTRUCTOR(int i):i(i){}
COPY_CONSTRUCTOR(other):i(other.i){}
void CLASS::fun1()
{
//...
}
For that, currently I need to copy&paste three #define lines. I would rather do those automatically. Is there any way to pull this off using the preprocessor?
Upvotes: 1
Views: 511
Reputation: 362087
No, such a DEF_CLASS(x)
macro is not possible. Macros aren't stateful. The best you can do is:
#define CONSTRUCTOR CLASS::CLASS
#define COPY_CONSTRUCTOR(x) CONSTRUCTOR(const CLASS& x)
...
#define CLASS X
CONSTRUCTOR(int i):i(i){}
COPY_CONSTRUCTOR(other):i(other.i){}
void CLASS::fun1()
{
//...
}
#undef CLASS
#define CLASS Y
CONSTRUCTOR(int i):i(i){}
COPY_CONSTRUCTOR(other):i(other.i){}
void CLASS::fun1()
{
//...
}
#undef CLASS
But I strongly urge you not to abuse the preprocessor like this. Why obfuscate the normal C++ syntax with these macros? Accept C++ for what it is; don't try to make it into a different, friendlier language.
Upvotes: 1
Reputation: 490653
No -- §16.3.4/3:
The resulting completely macro-replaced preprocessing token sequence is not processed as a preprocessing directive even if it resembles one [...]
Upvotes: 2