1 assert and verify macros work basically the same in debug mode. Both of them calculate the expression value. If the value is not 0, nothing will be done. If the value is 0, the diagnostic information is output.
2 assert and verify macros have different effects in the release mode. Assert does not calculate the expression value or output diagnostic information. Verify calculates the expression value, but does not output diagnostic information no matter whether the value is 0 or not.
Verify and assert are essentially different in program debugging.
In the debug version of MFC, The Verify macro evaluates its argument. If the result is 0,
The macro prints a Diagnostic message and halts the program. If the condition is nonzero,
It does nothing.
In the release version of MFC, verify evaluates the expression but does not print or interrupt the program. For example, if the expression is a function call, the call will be made.
Assertion Type Definition
Ansi c asserted void assert (INT expression );
C Runtime lib assert (booleanexpression );
_ Asserte (booleanexpression );
MFC assert (booleanexpression );
Verify (booleanexpression );
Assert_valid (pobject );
Assert_kindof (classname, pobject );
ATL assertion atlassert (booleanexpression );
In addition, the compilation of the trace () macro is also controlled by _ debug.
All these assertions are compiled only in the debug version, but are ignored in the release version. The only exception is verify (). In fact, these macros call the assert () function, but attach some library-related debugging code. If you add any program code to these macros, instead of a Boolean expression (for example, a value assignment or a function call that can change the value of a variable), the release version will not perform these operations, this may cause errors. It is easy for beginners to make such mistakes, and the search method is also very simple, because these macros are listed above, you only need to use the find in files function of VC ++ to find the place where these macros are used in all files of the project and then check them one by one. In addition, some experts may also add Conditional compilation such as # ifdef _ Debug. Pay attention to it.
By the way, it is worth mentioning that the verify () macro allows you to put program code in a Boolean expression. This macro is usually used to check the return values of Windows APIs. Some people may abuse verify () For this reason. In fact, this is dangerous because verify () violates the idea of assertions and cannot completely separate program code from debugging code, in the end, it may cause a lot of trouble. Therefore, experts suggest using this macro as little as possible.