Rec*_*als 4 c++ pragma region preprocessor-directive
在MSVC和C#中,#pragma region可用于标记代码段.
同样,在GCC/Clang中,#pragma mark可以完成同样的事情.
是否可以定义一个宏,例如CODELABEL(label)哪个宏适用于两个编译器?
基本上,我想避免必须执行以下操作:
#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();
Run Code Online (Sandbox Code Playgroud)
...而是做这样的事情:
CODELABEL( Variables )
bool MyBool;
int MyInt;
CODELABEL( Functions )
void MyMethod();
void AnotherMethod();
Run Code Online (Sandbox Code Playgroud)
这样的事情可能吗?
是的,在C++ 11中,您可以使用_Pragma,因为#pragma不允许在宏定义中使用:
#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
Run Code Online (Sandbox Code Playgroud)
舞蹈PRAGMA是满足_Pragma要求字符串文字,其中两个字符串文字(例如,"mark" "section label")的并排连接不起作用.