1

I want to do something like:

#define TYPE uint32_t
#define ADDSUFFIX(x) xTHETYPE

THETYPE * ADDSUFFIX(getvalue) (THETYPE * pMem) {}

And I need to get uint32_t getvalueuint32_t (uint32_t * pMem) {} depends on what TYPE is.

How to make this work ?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Galaxy
  • 1,862
  • 1
  • 17
  • 25

1 Answers1

2

You want the "token pasting" operator, but it can get tricky when one of the operands is a parameter or a macro. This worked for me:

#define THETYPE uint32_t
#define ADDSUFFIX_2(x,y) x ## y
#define ADDSUFFIX_1(x,y) ADDSUFFIX_2(x,y)
#define ADDSUFFIX(x) ADDSUFFIX_1(x,THETYPE)

THETYPE * ADDSUFFIX(getvalue) (THETYPE * pMem) {}

If you want getvalue_uint32_t, this works:

#define ADDSUFFIX(x) ADDSUFFIX_1(x ## _,THETYPE)

Also consider:

#define MKFUNC(type,func,param) \
type * ADDSUFFIX_1(func ## _,type) (type * param)
MKFUNC(uint32_t,getvalue,pMem) { }
Random832
  • 37,415
  • 3
  • 44
  • 63
  • It seems a common problem. I just found http://stackoverflow.com/questions/1597007/creating-c-macro-with-and-line-token-concatenation-with-positioning-macro – Galaxy May 27 '11 at 02:00
  • I want to use `gcc -D U32 -c x.c -o x32.o` and `gcc -D U64 -c x.c -o x64.o`. And I think this should be a way of implying template functions in C. – Galaxy May 27 '11 at 02:15