-----Original Message----- From: boost-users-bounces@lists.boost.org [mailto:boost-users-bounces@lists.boost.org] On Behalf Of Rush Manbert
I posted this question to comp.lang.c++.moderated and got a single reply that basically said "it looks like you need to use macros", but I thought I would ask the same question here, just in case you MPL folks have some tricks up your boost library sleeves.
Just so you know, many Boost libraries, including the MPL, make extensive use of macros for code generation purposes.
IMHO, the benefit of this technique is that you only maintain CODE_TABLE. All of the required code bits are generated from it. The downside is that Macros are Evil,
Macros are not evil. Some uses of them are evil; some are not.
the macro-that-is-a-list-of-macros is confusing,
It is actually fairly straightforward.
and you need to carefully redefine CODE(codeValue) in the proper locations. Additionally, the real application will have multiple classes that each define their own CODE_TABLE contents, so making it all work correctly is a little tricky.
The only thing that is potentially confusing is the #define/#undef of
CODE--whose use is hidden behind the the CODE_TABLE interface. It would be
better, IMO, to get rid of that reliance on a specific name (i.e. "CODE"). E.g.
// foo.h
#include
My question is this: Is there any method available in C++ that can let me do away with the macros while preserving the nice quality that only one data structure needs to be maintained?
Only the preprocessor can do the stringizing. You can't initialize an array statically with templates (unless you know the exact size). If that isn't an absolute requirement, you can do the rest with templates.
Something sophisticated and tricky with templates perhaps? I expect that the fact that I stringize the enum name when creating the lookup table makes a macro the only choice. But, since that is only one component in each lookup table entry in the real code, I might be willing to give it up in order to lose the macros.
There is nothing wrong with using macros for this. You could do most of the above with the template mechanism, but it might very well turn out to be more complex (and therefore, more confusing). For something like this, I'd go with the preprocessor. It is simple and direct--you have some code that needs to be written (and maintained) in accordance with a dataset, but you don't want to write the code manually (and, more importantly, maintain manually). Therefore, let the preprocessor automate the writing. It really is straightforward--the preprocessor is just writing what you otherwise would be writing by hand. Regards, Paul Mensonides