Cross-platform code label tag macro?

In MSVC and C # #pragma regioncan be used to indicate a section of code.
Similarly, in GCC / Clang, it #pragma markcan do the same thing.

Is it possible to define a single macro, such as one CODELABEL(label), that will work for both compilers?

Basically, I would like to not do the following:

#ifdef _WIN32
#pragma region Variables
#else
#pragma mark Variables
#endif
bool MyBool;
int MyInt;

#ifdef _WIN32
#pragma region Methods
#else
#pragma mark Methods
#endif
void MyMethod();
void AnotherMethod();

... and instead do something like this:

CODELABEL( Variables )
bool MyBool;
int MyInt;
CODELABEL( Functions )
void MyMethod();
void AnotherMethod();

Is this possible?

+4
source share
2 answers

Yes, in C ++ 11 you can use _Pragma, since use #pragmain a macro definition is not allowed:

#ifdef _WIN32
#define PRAGMA(x) __pragma(x) //it seems like _Pragma isn't supported in MSVC
#else
#define PRAGMA(x) _Pragma(#x)
#endif

#ifdef _WIN32
#define CODELABEL(label) PRAGMA(region label)
#else
#define CODELABEL(label) PRAGMA(mark label)
#endif

PRAGMA _Pragma, , (, "mark" "section label") .

+5

.

#define STR_HELPER(x) #x
#define STR(x) STR_HELPER(x)

#ifdef _WIN32
  #define LABEL region
#else
  #define LABEL mark
#endif

#pragma STR(LABEL) Variables
bool MyBool;
int MyInt;
#pragma STR(LABEL) Functions
void MyMethod();
void AnotherMethod();
+1

Source: https://habr.com/ru/post/1606514/


All Articles