Vous pouvez simuler conditionals à l'aide macro token concatenation comme suit:
#define DEF_CONST(b_const) DEF_CONST_##b_const
#define DEF_CONST_true const
#define DEF_CONST_false
Ensuite,
/* OK */
DEF_CONST(true) int x; /* expands to const int x */
DEF_CONST(false) int y; /* expands to int y */
/* NOT OK */
bool bSomeBool = true; // technically not C :)
DEF_CONST(bSomeBool) int z; /* error: preprocessor does not know the value
of bSomeBool */
En outre, ce qui permet de passer des paramètres macro se DEF_CONST (comme l'a fait correctement par GMan et d'autres):
#define DEF_CONST2(b_const) DEF_CONST_##b_const
#define DEF_CONST(b_const) DEF_CONST2(b_const)
#define DEF_CONST_true const
#define DEF_CONST_false
#define b true
#define c false
/* OK */
DEF_CONST(b) int x; /* expands to const int x */
DEF_CONST(c) int y; /* expands to int y */
DEF_CONST(true) int z; /* expands to const int z */
Vous pouvez également considérer le beaucoup simp ler (bien que potentiellement moins flexible):
#if b_const
# define DEF_CONST const
#else /*b_const*/
# define DEF_CONST
#endif /*b_const*/
Quel est le point de tout cela? Avez-vous un exemple concret, car cela semble plutôt étrange ... –
Voir aussi. –
Rhubbarb