I saw somewhere assert used with a message in the following way:
assert(("message", condition));
This seems to work great, except that gcc throws the following warning:
warning: left-hand operand of comma expression has no effect
How can I stop the warning?
If you want to pass a formatted message, you could use the following macros:
Then use it like printf:
Output:
Based on http://c.learncodethehardway.org/book/ex20.html
You could write your own macro that provides the same usage of
_Static_assert(expr, msg)
:I also have a macro
warn_bug()
that prints the name of the program, the file, the line, the function, the errno value and string, and a user message, even if asserts are disabled. The reason behind it is that it won't break the program, but it will warn that a bug will probably be present. You could just defineassert_msg
to be empty ifdefined(NDEBUG)
, though.According to following link http://www.cplusplus.com/reference/clibrary/cassert/assert/
assert is expecting only expression. May be you are using some overloaded function.
According to this, only expression is allowed and thus you are getting this warning.
Use
-Wno-unused-value
to stop the warning; (the option-Wall
includes-Wunused-value
).I think even better is to use another method, like
Try:
use as such:
Will execute the block only when assert fails.
By tradition,
(void)
communicates to the compiler that you are knowingly ignoring an expression: