Vorac
Vorac

Reputation: 9122

How does the standard C++ assert() work and how do I redefine it?

In assert.h we can see

# define assert(__e) ((__e) ? (void)0 : __assert_func (__FILE__, __LINE__, \
                               __ASSERT_FUNC, #__e))

Because the target is embedded, I need to actually display the error message upon assertion failure, so I need to invoke the LCD write function. I tried the following:

void handleCriticalError(int err_num, char *fname, int line_num, const char *foo); // Halts the program!
#define ASSERT(__e) ((__e) ? (void)0 : handleCriticalError (0, __FILE__, __LINE__, __func__))

However, when I use it, I get a strange error.

ASSERT(1<2);
>error: deprecated conversion from string constant to 'char*' [-Werror=write-strings]

How do I tie my function to a custom assert, or to the standard assert, so that the function is called upon failed assertion?

Note that I do not need a full-featured, standard-adhering assert as is discussed here. I am going to use it only as single statements on a line, with a simple comparison of variables or constants.

Upvotes: 0

Views: 3539

Answers (2)

sth
sth

Reputation: 229894

The file name is probably a string constant (const char*), but your function takes a char* parameter. This mismatch causes the error.

If you declare the function parameter as const char *fname instead, the error should go away.

Upvotes: 5

dmitri
dmitri

Reputation: 3294

Redefine you function:

void handleCriticalError(int err_num, char const *fname, int line_num, const char *foo);

note const *fname parameter that should accept __FILE__

Upvotes: 3

Related Questions