Assert
Assert is a pre-processing macro that is managed by the preprocessor instead of the compiler, so it is not used without namespace declarations, but if you write Std::assert it is wrong. Use an assert to include Cassert or assert.h, using the following:
assert(expr)
The main purpose of the Assert is: for debugging, to detect some situations that should not occur . If expr is false (that is, 0), the assert outputs the information and terminates the operation of the program, and if expr is true, then what is done.
The use of assert relies on a macro ndebug (no Debug), and if Ndebug is undefined, assert works, and if Ndebug is defined, assert does not work at all. So when using VS development, the debug compile-time assert is valid and release compile-time assert is invalid.
Example code:
void test(){ intid = -2; assert(id0);}
The general ID is not negative, so we use the assert to detect whether the ID has a negative number, if the ID is negative, run in debug mode will pop up the following warning box, run in release mode will not happen.
The advantage of this is that the program can find errors in debugging.
* * Note: **assert should be used only to verify things that are not likely to happen, instead of using it instead of a real run-time logic check, or in lieu of error checking that the program itself should contain.
preprocessing variables
Similar to assert, the preprocessor also defines variables that are used to hold the source code, listing only the commonly used preprocessing variables:
file : The path and file name where the source code resides;
Line: Number of lines where the code is located
functions: Function name of the function where the code resides
Date: Day when the file was compiled
Time: When the file was compiled
This information is useful in debugging and logging, and can define exactly where the error is located.
Example code:
void test(){ "in "__FILE__" file, "__LINE__" line " << endl; "in "" function." << endl; "at "__DATE__" "__TIME__ << endl;}
The result of running in debug mode is as follows:
Copyright NOTICE: This article for Bo Master original article, without Bo Master permitted not for any commercial use, reproduced please indicate the source.
Assert preprocessing macros and preprocessing variables