Kron
Kron

Reputation: 173

In macro #define can we use #pragma push_macro and pop_macro

My goal is to remove name from SECTION_STOP in the following macros:

// Start creates the object
#define SECTION_START(name, ...) \
  auto section_##name = create_section_name(#name, ## __VA_ARGS__);

// Stop destroys the object
#define SECTION_STOP(name) \
  section_##name.reset();

I see that C++ macros support both the COUNTER operator and a concept of pushing and popping macro names.

I have a rough idea:

// Start creates the object
#define SECTION_START(name, ...) \
  #define CURRENT_SECTION_COUNTER __COUNTER__ \
  auto section_##CURRENT_SECTION_COUNTER = create_section_name(#name, ## __VA_ARGS__); \
  _Pragma("push_macro(\"CURRENT_SECTION_COUNTER\")") \
  #undef CURRENT_SECTION_COUNTER

// Stop destroys the object
#define SECTION_STOP() \
  _Pragma("pop_macro(\"CURRENT_SECTION_COUNTER\")") \
  section_##CURRENT_SECTION_COUNTER.reset();

The above wouldn't work because there are a number of issues with it (can't have nested #define and need some sort of stringify operator). But is it possible to leverage __COUNTER__ (or something similar) alongside #pragma push_macro/pop_macro to achieve what I am after?

Upvotes: 2

Views: 106

Answers (0)

Related Questions