Is there a way to add or edit the message thrown by assert? I'd like to use something like
assert(a == b, "A must be equal to B");
Then, the compiler adds line, time and so on...
Is it possible?
Is there a way to add or edit the message thrown by assert? I'd like to use something like
assert(a == b, "A must be equal to B");
Then, the compiler adds line, time and so on...
Is it possible?
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:Since
assert
shows the condition that failed, it will display your message too. If it's not enough, you can write your ownmyAssert
function or macro that will display whatever you want.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:
(this was copied from above comments, for better visibility)
Here's my version of assert macro, which accepts the message and prints everything out in a clear way:
Now, you can use this
And in case of failure you will get a message like this:
Nice and clean, feel free to use it in your code =)
Why nobody mentioned the cleanest solution?
For vc, add following code in assert.h,
assert is a macro/function combination. you can define your own macro/function, using
__FILE__
,__BASE_FILE__
,__LINE__
etc, with your own function that takes a custom message