Re: Nested macros
On May 15, 8:21 am, The D0ct0r <thed0c...@xs4all.nl> wrote:
please consider the following:
#if DEBUG
#define SOMEMACRO(type, name, value) (type) (name) = (value)
#else
#define SOMEMACRO(type, name, value) #define (name) (value)
#endif
If I define DEBUGging, i want the variable (name) to be a
variable, but if not, I want it to be a constant. When I
compile this, the preprocessor starts complaining at the
fourth line, because of the second #define.
The results of macro expansion are not processed as
pre-processing declarations, even if they look like one.
Is this OT here?
No.
And if not, how should I solve this?
You can't use a macro to generate a #define. On the other hand,
you don't want to use #define for constants in C++ anyway.
Something like:
#if DEBUG
#define SOMEMACRO( type, name, value ) type name = value
#else
#define SOMEMACRO( type, name, value ) type const name value
#endif
would probably work. (Note that you cannot put parentheses
around the type. A declaration like:
(int) (toto) = (10) ;
is not legal. You might also want to make the variables
static.)
Also, I very seriously question the wisdom of this. If there's
any reason for the values to not be const, don't make them
const. And if there's not, they should be const in the debug
build as well.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34