C++ assert
Assert is a great tool for debugging your c++ code.Let's have look the following codes.
#include <iostream>
#include <functional>
#include <assert.h>
using namespace std;
int main()
{
int a = 10;
assert(a >10);
char b ='a';
cout << b << endl
}
If I compile and run it, I get the following message
Assertion failed: (a >10), function main, file main.cpp, line 10.
Abort trap: 6
int main()
{
int a = 10;
assert(a ==10);
char b ='a';
cout << b << endl
}
If I compile and run it, it will display a in the monitor
As you have guessed, assert helps you test variable.
You need to put a condion for assert.
If the condition is true, nothing is going to happen.
If it is false, assert terminates program, and tell us what the problem is.
If you create couple hundreds lines of code, I am sure that you would like to test some critical parts of your work. Why don't you use assert?
In my opinion, assert is a good tool for c++ development.
If you disagree or have better tool, please let me know.
No comments:
Post a Comment