C++ C++;函数中的宏和默认参数

C++ C++;函数中的宏和默认参数,c++,macros,mingw,default-arguments,C++,Macros,Mingw,Default Arguments,我正在尝试创建一个显示错误消息的通用函数,在消息显示后程序可能会退出 我希望函数显示发生错误的源文件和行 参数列表: 1.char *desc //description of the error 2.char *file_name //source file from which the function has been called 3.u_int line //line at which the function has been called 4.bool bexit=false //

我正在尝试创建一个显示错误消息的通用函数,在消息显示后程序可能会退出

我希望函数显示发生错误的源文件和行

参数列表:

1.char *desc //description of the error
2.char *file_name //source file from which the function has been called
3.u_int line //line at which the function has been called
4.bool bexit=false //true if the program should exit after displaying the error
5.int code=0 //exit code
由于(4)和(5),我需要在函数定义中使用默认参数,因为我不希望指定它们,除非程序应该退出

由于(2)和(3),我需要使用一个重定向到原始函数的宏,如下所示:

#define Error(desc, ???) _Error(desc,__FILE,__LINE__, ???)
问题是我不知道这两个元素应该如何协同工作

它应该是什么样子的示例:

if(!RegisterClassEx(&wndcls))
    Error("Failed to register class",true,1); //displays the error and exits with exit code 1

if(!p)
    Error("Invalid pointer"); //displays the error and continues

您不能在C99中重载宏——您将需要两个不同的宏。有了C11,使用
\u Generic
还有一些希望

我使用宏开发了一个非常类似的东西——Visual Studio的自定义警告生成器片段。GNU GCC有一些类似的设置,以便与MSV兼容

#define STR2(x) #x
#define STR1(x) STR2(x)
#define LOC __FILE__ “(“STR1(__LINE__)”) : Warning Msg: “
#define ERROR_BUILDER(x) printf(__FILE__ " (“STR1(__LINE__)”) : Error Msg: ” __FUNCTION__ ” requires ” #x)
以上几行处理你的论点1到3。添加对4的支持需要在宏中插入一个
exit()
调用。此外,如果需要创建两个不同的宏包装器,以创建两个不同的参数列表(具有默认参数的一个可以委托给另一个宏)

我已经做了一个详细的描述(警告:这是我的博客——所以认为它是一个无耻的插件)。

(也许)在VAYARGS之前加上“y'y'”,我以前尝试过使用变量宏,但是没有γi,它就不起作用了,谢谢。
#define ERROR_AND_EXIT(msg, cond, errorcode) ERROR_BUILDER(msg) if (cond) exit(errorcode)
#define ERROR_AND_CONT(msg) ERROR_BUILDER(msg)