-
Notifications
You must be signed in to change notification settings - Fork 2
TH_ASSERT
Tobias Widlund edited this page Nov 3, 2015
·
2 revisions
View the code in the repository
Assertions are a useful way of ensuring that the internal state is sane and catching bugs early. However the default assert() from tion has a couple of problems which this assertion macro solves. The issues addressed are:
- assert() shows no file/function/line information
- assert() does not allow for composing error messages by resolving expressions
- assert() does not trigger a break in gdb, but terminates the application instantly, preventing the developer to investigate the stack
TH_ASSERT is very straight-forward, taking only two parameters: The assertion statement (which is counted as erronious if it resolves into false) and an std::string with the message shown in case of error.
Note that as with the normal assert(), TH_ASSERT is a null operation in release builds (if -NDEBUG is defined).
Example:
//on line 5 of main.cpp in main()
TH_ASSERT(false, "Oops, this is an error with error code " + std::to_string(5));
Output in debug mode:
Assertion `false` failed in main.cpp function main line 5: Oops, this is an error with error code 5