2011-10-17 27 views
9

Me gustaría evaluar un token antes de que se concatene con algo más. El "problema" es que la norma especifica el comportamiento comoEvaluar token preprocesador antes de ## concatenación

antes de la lista de reemplazo se volvió a examinar los nombres más macro a sustituir, cada instancia de un token de preprocesamiento ## en la lista de reemplazo (no de un argumento) se elimina y el token de preprocesamiento previo se concatena con el siguiente token de preproceso.

por lo tanto, en el siguiente ejemplo,

#include <stdlib.h> 

struct xy { 
    int x; 
    int y; 
}; 

struct something { 
    char * s; 
    void *ptr; 
    int size; 
    struct xy *xys; 
}; 
#define ARRAY_SIZE(a) (sizeof(a)/sizeof((a)[0])) 

#define DECLARE_XY_BEGIN(prefix) \ 
struct xy prefix ## _xy_table[] = { 

#define XY(x, y) {x, y}, 

#define DECLARE_XY_END(prefix) \ 
    {0, 0} \ 
}; \ 
struct something prefix ## _something = { \ 
    "", NULL, \ 
    ARRAY_SIZE(prefix ## _xy_table), \ 
    &(prefix ## _xy_table)[0], \ 
}; 

DECLARE_XY_BEGIN(linear1) 
    XY(0, 0) 
    XY(1, 1) 
    XY(2, 2) 
    XY(3, 3) 
DECLARE_XY_END(linear1) 


#define DECLARE_XY_BEGIN_V2() \ 
struct xy MYPREFIX ## _xy_table[] = { 

#define DECLARE_XY_END_V2() \ 
    {0, 0} \ 
}; \ 
struct something MYPREFIX ## _something = { \ 
    "", NULL, \ 
    ARRAY_SIZE(MYPREFIX ## _xy_table), \ 
    &(MYPREFIX ## _xy_table)[0], \ 
}; 

#define MYPREFIX linear2 
DECLARE_XY_BEGIN_V2() 
    XY(0, 0) 
    XY(2, 1) 
    XY(4, 2) 
    XY(6, 3) 
DECLARE_XY_END_V2() 
#undef MYPREFIX 

La última declaración se expande en

struct xy MYPREFIX_xy_table[] = { 
{0, 0}, 
{2, 1}, 
{4, 2}, 
{6, 3}, 
{0, 0} }; struct something MYPREFIX_something = { "", 0, (sizeof(MYPREFIX_xy_table)/sizeof((MYPREFIX_xy_table)[0])), &(MYPREFIX_xy_table)[0], }; 

y no

struct xy linear2_xy_table[] = { 
{0, 0}, 
{2, 1}, 
{4, 2}, 
{6, 3}, 
{0, 0} }; struct something linear2_something = { "", 0, (sizeof(linear2_xy_table)/sizeof((linear2_xy_table)[0])), &(linear2_xy_table)[0], }; 

como que quiero. ¿Hay alguna forma de definir macros que produzca esto? El primer conjunto de macros sí lo hace, pero me gustaría evitar la duplicación de prefijos y solo tenerlo definido una vez. Entonces, ¿es posible establecer el prefijo con #define y dejar que las macros lo usen?

+0

posible duplicado de [Cómo concatenar dos veces con el preprocesador C y ampliar una macro como en "arg ## \ _ # # MACRO "?] (Http://stackoverflow.com/questions/1489932/how-to-concatenate-twice-with-the-c-preprocessor-and-expand-a-macro-as-in-arg) Por favor, intente para minimizar los ejemplos :-) –

Respuesta

4

Puede usar la expansión de segundo nivel para esto, ej.

#define XY_HLP1(a) DECLARE_XY_BEGIN(a) 
#define XY_HLP2(a) DECLARE_XY_END(a) 
#define DECLARE_XY_BEGIN_V2() XY_HLP1(MYPREFIX) 
#define DECLARE_XY_END_V2() XY_HLP2(MYPREFIX) 
+0

Sí, me gusta esto. No es necesario cambiar las macros antiguas y crear posibles problemas de fusión para otros, y aunque la expansión doble no sea intuitiva, sigue siendo obvio que se usa el mismo prefijo en ambos lugares. – hlovdal

11

You can use a macro for concatenation como

#define CONCAT_(A, B) A ## B 
#define CONCAT(A, B) CONCAT_(A, B) 

esto funciona entonces

#define A One 
#define B Two 
CONCAT(A, B) // Results in: OneTwo 
+1

Nunca entendí por qué tales cosas requieren dos niveles. ¿Es el primer nivel para la expansión macro y el segundo para la concatenación real? – ijustlovemath

Cuestiones relacionadas