0

The ## are not working with an string an a macro. My compiler not extract the value of the macro or something else after ##

#define ProfilerScope(name) Timer timer ## __LINE__;

This just returns me Timer timer__LINE__

I tried to make a STRINGIZE macro to makes this works but nothing, they just returns me the same

#define STRINGIZE(x) STRINGIZE_SIMPLE(x)
#define STRINGIZE_SIMPLE(x) #x

#define ProfilerScope(name) Timer timer ## STRINGIZE(__LINE__);

return Timer timerSTRINGIZE(__LINE__)

I want it to return the line of code where I'm using the macro

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
AletropyZ
  • 3
  • 1
  • 1
    `STRINGIZE` adds quotes, and `Timer timer42;` is supposed to have no quotes. But notice how `STRINGIZE` uses two macros instead of one. You must do the same: pass `__LINE__` from one macro to another, then use `##` in the second macro. – HolyBlackCat Jan 22 '23 at 13:28
  • `##` operator applies after parameter substitution but before the list is reprocessed for further macro replacements. You need a couple levels of indirection, with `##` at the very end. [Something like this](https://godbolt.org/z/9hqcbM1cf) – Igor Tandetnik Jan 22 '23 at 13:29
  • `This just returns me Timer timer__LINE__` How do you know? – KamilCuk Jan 24 '23 at 08:47

1 Answers1

0

I want it to return the line of code where I'm using the macro

I think you want to expand __LINE__ and then concatenate it with a word. You have to expand it in a macro. The following code:

#define CONCAT(a, b)  CONCAT2(a, b)
#define CONCAT2(a, b)  a##b
#define ProfilerScope(name) Timer CONCAT(timer, __LINE__);
ProfilerScope()

outputs Timer timer4;.

KamilCuk
  • 120,984
  • 8
  • 59
  • 111