Write your own assert () macro (helpful for debugging programs)

Source: Internet
Author: User

Program code:

# Include <iostream> using namespace STD; # define debug # ifndef debug # define assert (x) # else # define assert (x) if (! (X) {cout <"error! Assert ("<# x <") Macro function execution failed; "; cout <" error code appears in row "<_ line _ <; "; cout <" the file with an error is: "<_ file __< <" \ n ";}# endifint main () {int x = 999; cout <"the first execution of assert ():" <Endl; Assert (x = 999); cout <"the second execution of assert ():" <Endl; assert (X! = 999); cout <"program ended" <Endl; return 0 ;}

Note:

1. The assert () macro uses the _ line _ and _ file _ macros to determine the number of lines of the error code and the file where the error code is located.


2. The forward slash "\" at the end of the line connects the Statement of the line with the following one sentence.


3. The # X: # Operator quotes any character after it. Here is "X ".


4. debug is the debugging switch. Generally, when debug is enabled, it takes effect only when assert () is enabled. After the program is checked, the debug will be closed if there is no error, so that the assert () will be deleted.

Therefore, the above program first defines a debug, which is generally completed by the compiler. The debug option of the compiler enables or disables debug, its function is to define or cancel the definition of a debug. Then, judge whether the debug is defined. If not, define the macro function assert (x), which has no function, otherwise, a macro function is defined to throw an exception.


5. Use the upper case method to distinguish the default assert () of the system (). Some compilers provide assert () macros, which are translated as assertions. The function of assert () is to return true when the assert () parameter is true. If the parameter value is false, some compilers abort the program when the assert () function fails. Other compilers throw an exception, for example, output an error message.

Write your own assert () macro (helpful for debugging programs)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.