Reputation: 817
Can I do something like this:
#define VERSION 4_1
int32_t myVersion??VERSION;
// What I expect here is that the variable should be generated with name myVersion4_1.
// If possible what should be placed instead of ?? above?
Is it possible to form variable name using Macro like above in C++?
Upvotes: 1
Views: 145
Reputation: 48527
Not exactly how you try, but you can do what follows:
#define VAR_VERSIONED_NAME(name) name##4_1
int32_t VAR_VERSIONED_NAME(myVersion) = 1;
myVersion4_1 = 2;
or if a VERSION
must be a separate define
:
#define VERSION 4_1
#define CAT_I(a, b) a ## b
#define CAT(a, b) CAT_I(a, b)
#define VAR_VERSIONED_NAME(name) CAT(name, VERSION)
int VAR_VERSIONED_NAME(myVersion) = 1;
myVersion4_1 = 2;
Upvotes: 3
Reputation:
You need a level of indirection to expand VERSION
before you can paste it.
#define VERSION 4_1
#define expand(v) paste(v)
#define paste(v) myVersion ## v
int main()
{
int expand(VERSION);
myVersion4_1 = 42;
}
Upvotes: 1