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
  • 32,368
  • 48
  • 194
  • 335

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
  • 203,559
  • 14
  • 181
  • 302