Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function name built from macro using pre-defined string and argument

Tags:

c++

macros

I'm currently having a problem with the definition of a macro in C++.

I want it like this:

#define MY_MACRO (Name, Address) __int32 Get_Name() { return Address; }

Now, when I call it like this:

MY_MACRO(Test, 0x10);

it spits out

__int32 Get_Name() { return 0x10; }
            ^^^^

instead of

__int32 Get_Test() { return 0x10; }
            ^^^^

How can I solve this issue? I really need the Get_ in the name, and then the name passed by the argument.

like image 605
hresult Avatar asked Feb 04 '26 05:02

hresult


2 Answers

Use the macro concatenation operator.

#define MY_MACRO (Name, Address) __int32 Get_##Name() { return Address; }
like image 111
Qix - MONICA WAS MISTREATED Avatar answered Feb 05 '26 18:02

Qix - MONICA WAS MISTREATED


You have to concatenate strings this way

#define MY_MACRO(Name, Address) __int32 Get_##Name() { return Address; }
like image 36
4pie0 Avatar answered Feb 05 '26 19:02

4pie0