在宏中使用 __FUNCSIG__ 将其扩展为空
using __FUNCSIG__ in a macro expands it to be empty
我正在使用 API 其中 return int 错误代码,我决定构建一个包装器错误 class :
class Error
{
...
public:
Error(int ErrC, const char* UserMessage, const char* FileName, int LineNumber, const char* Function);
char * GetFunction();
...
}
我决定冒险进入宏的世界并创建一个宏来为我实例化 class :
#define API_ERROR(Code,MSG) API::Error(Code,MSG,__FILE__,__LINE__,__FUNCSIG__)
然后我定义了一个由 main
调用的测试函数
void TestFunc()
{
API::Error Error = API_ERROR(0,"Hello");
std::cout << Error.GetFunction();
}
使用编译器选项输出post预处理结果(Properties->C/C++->Preprocessor->Preprocess to a file)产生
void TestFunc()
{
API::Error Error = API::Error(o,"Hello","...\main.cpp",30,);
std::cout << Error.GetFunction();
}
我认为这不起作用,因为 __FUNCSIG__
仅在函数内部定义。
我也试过
#define EMPTY()
#define DEFER(...) __VA_ARGS__ EMPTY()
#define API_ERROR(Code,MSG) API::Error(Code,MSG,__FILE__,__LINE__,DEFER(__FUNCSIG__))
但我想我误解了 post 的作者。
有没有办法让它工作?
我正在使用带有默认 MSVC++ 编译器的 Visual Studio 2019 社区。
来自this VS2019 predefined macro reference
The __FUNCSIG__
macro isn't expanded if you use the /EP
or /P
compiler option.
[强调我的]
如果您对源代码进行预处理,则会设置 /EP
或 /P
标志,并且不会展开宏。它只会在实际构建源代码时展开。
预处理器可能不会扩展 __FUNCSIG__
宏,因为它对 C++ 符号一无所知。它甚至可能不是真正的预处理器宏,当 C++ 符号已知时,可以在编译的后期 "expanded"(或替换)。
我正在使用 API 其中 return int 错误代码,我决定构建一个包装器错误 class :
class Error
{
...
public:
Error(int ErrC, const char* UserMessage, const char* FileName, int LineNumber, const char* Function);
char * GetFunction();
...
}
我决定冒险进入宏的世界并创建一个宏来为我实例化 class :
#define API_ERROR(Code,MSG) API::Error(Code,MSG,__FILE__,__LINE__,__FUNCSIG__)
然后我定义了一个由 main
调用的测试函数void TestFunc()
{
API::Error Error = API_ERROR(0,"Hello");
std::cout << Error.GetFunction();
}
使用编译器选项输出post预处理结果(Properties->C/C++->Preprocessor->Preprocess to a file)产生
void TestFunc()
{
API::Error Error = API::Error(o,"Hello","...\main.cpp",30,);
std::cout << Error.GetFunction();
}
我认为这不起作用,因为 __FUNCSIG__
仅在函数内部定义。
我也试过
#define EMPTY()
#define DEFER(...) __VA_ARGS__ EMPTY()
#define API_ERROR(Code,MSG) API::Error(Code,MSG,__FILE__,__LINE__,DEFER(__FUNCSIG__))
但我想我误解了 post 的作者。
有没有办法让它工作?
我正在使用带有默认 MSVC++ 编译器的 Visual Studio 2019 社区。
来自this VS2019 predefined macro reference
The
__FUNCSIG__
macro isn't expanded if you use the/EP
or/P
compiler option.
[强调我的]
如果您对源代码进行预处理,则会设置 /EP
或 /P
标志,并且不会展开宏。它只会在实际构建源代码时展开。
预处理器可能不会扩展 __FUNCSIG__
宏,因为它对 C++ 符号一无所知。它甚至可能不是真正的预处理器宏,当 C++ 符号已知时,可以在编译的后期 "expanded"(或替换)。