Assert preprocessing macros and preprocessing variables

Source: Internet
Author: User

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

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.