1

Consider the following code:

struct S {};

#define CREATE_INSTANCE S instance_##__LINE__

int main()
{
    CREATE_INSTANCE;
    CREATE_INSTANCE;
    return 0;
}

What I want it to do is create two instances of S named instance_7 and instance_8. What it actually does is creates instance___LINE__ twice.

How to achieve what I want?

Violet Giraffe
  • 31,078
  • 43
  • 182
  • 317

1 Answers1

2

Using some indirection:

#define Concat_(a, b) a ## b
#define Concat(a, b) Concat_(a, b)
#define CREATE_INSTANCE S Concat(instance_, __LINE__)
Jarod42
  • 190,553
  • 13
  • 166
  • 271