C macro: concatenate symbols conditonally -


i have

#define a_t 1 #define b_t 2 int x_a = 1, x_b =2; 

how can define macro, can concatenate suffix _a , _b var name?

for example, #define a_t_suf _a #define b_t_suf _b #define suffix(t) t ## _suf  #define var_suf(var, t) var ## suffix(t)  ..... var_suf(x, a_t) ---> replaced x_a 

is possible?

you need indirection in var_suf macro force evaluate called macros before concatenating tokens instead of concatenating first:

#define a_t_suf _a #define b_t_suf _b #define suffix(t) t ## _suf  #define cat(a, b) ## b #define xcat(a, b) cat(a, b) #define var_suf(var, t) xcat(var, suffix(t))  ..... var_suf(x, a_t) ---> replaced x_a 

without indirect, var_suf(x, a_t) expand xsuffix(a_t) (concatenate first, more macros). cat/xcat indirection, expand suffix(a_t) first , concatenate.

xcat short expand_and_concatenate, while cat concatenate (without expansion.)

edit

if a_t macro (eg, #define a_t 1) replaced first. can avoid removing indirection of ## in suffix macro:

#define a_t_suf _a #define b_t_suf _b #define cat(a, b) ## b #define xcat(a, b) cat(a, b) #define var_suf(var, t) xcat(var, t##_suf)  ..... var_suf(x, a_t) ---> replaced x_a 

this cause concatenation happen first, macros expanded, other concatenation happen

if x macro, have problem, there's no way expand 1 token , not other before concatenate them.


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -