C ++ macros and default arguments in a function

I am trying to make a general function for displaying error messages with the possibility of a program exit after a message appears.

I want the function to display the source file and the line where the error occurred.

argument list:

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

Due to (4) and (5), I need to use the default arguments in the function definition, since I do not want them to be indicated if the program should not exit.

Due to (2) and (3), I need to use a macro that redirects to an unprocessed function, such as:

#define Error(desc, ???) _Error(desc,__FILE,__LINE__, ???)

The problem is that I do not see how these two elements should work together.

An example of what it should look like:

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
+3
source share
1 answer

C99 - . C11 _Generic.

- Visual Studio - . GNU GCC MSVS.

#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() . , , (, ).

#define ERROR_AND_EXIT(msg, cond, errorcode) ERROR_BUILDER(msg) if (cond) exit(errorcode)
#define ERROR_AND_CONT(msg) ERROR_BUILDER(msg)

(: - ).

+1

All Articles