This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: How to write a macro to concatenate two symbols without spaces between them?
- From: Andrew Haley <aph at redhat dot com>
- To: Pan ruochen <panruochen at gmail dot com>
- Cc: gcc-help at gcc dot gnu dot org
- Date: Fri, 31 Oct 2008 09:54:51 +0000
- Subject: Re: How to write a macro to concatenate two symbols without spaces between them?
- References: <af0faace0810302023of3dcfc2ta70b36b9eef8d336@mail.gmail.com>
Pan ruochen wrote:
> Hi All,
>
> Here is my code
> ---------------------------------------
> #define TYPE 1x1
> void func_1x1() { ... }
> void func_2x2() { ... }
> void func_3x3() { ... }
> ---------------------------------------
>
> I want to implement such an macro my_func(), which asscociats with the
> corresponding function automatically
> according to the value of another macro TYPE. For example, if TYPE is 1x1
> my_func() is expanded to func_1x1()
> and if TYPE is 2x2, my_func() is expanded to func_2x2()
> I tried #define my_func func ## TYPE
> and #define my_func func TYPE. They are not appropriate.
> How should I write this macro my_func()?
#define P(P1, P2) P1 ## P2
#define MY_FUNC(__TYPE) void P(func_, __TYPE) { }
#define TYPE 1x1
#define my_func MY_FUNC(TYPE)
my_func