Add custom messages in assert?
Solution 1:
A hack I've seen around is to use the &&
operator. Since a pointer "is true" if it's non-null, you can do the following without altering the condition:
assert(a == b && "A is not equal to B");
Since assert
shows the condition that failed, it will display your message too. If it's not enough, you can write your own myAssert
function or macro that will display whatever you want.
Solution 2:
Another option is to reverse the operands and use the comma operator. You need extra parentheses so the comma isn't treated as a delimiter between the arguments:
assert(("A must be equal to B", a == b));
(this was copied from above comments, for better visibility)