Assert usage, principle, adaptation (C ++)

Source: Internet
Author: User

From: http://hi.baidu.com/hplonline/blog/item/8637ab4470ee268bb3b7dcaa.html

 

I recently discovered that assert is so easy to use...
Let's look at how it is implemented and find some interesting things.

Usage:

First include
# Inlcude <assert. h>

Here is a sentence:
Assert (expression.
Expression is any valid logical expression.
For example:

File * fp = fopen ("in.txt", "R ");
If (! FP ){
Exit (0 );
}
Assert (FP! = NULL );

When expressions are not met, an ugly box is displayed,
Then, output the file and row number that the assert does not meet to the console.

When debugging is specific,
You can place assert that is considered to be a true expression in various places through the Internet,
Which one may have exploded, So I caught the opportunity to find the problem.

Principle:

As long as there is a source code, there is no secret,
So open assert. h and see how it is written.

Here are two main sentences:
_ Cribd void _ cdecl _ assert (void *, void *, unsigned );
# Define assert (exp) (void) (exp) | (_ assert (# exp, _ file __, _ line _), 0 ))

The first sentence is to output some information, and a box is displayed,
End by the wayProgramThese activities.
When called, it is similar:
_ Assert ("false", "C: \ 1.cpp", 15)
In this way.

The structure of the second sentence is simplified. A small sentence also contains many things that have not been noticed before.

1. Short Circuit Evaluation

This is an important feature of C. If it is false before processing &, you do not need to continue,
When processing |, if it is true, you do not need to continue.
Visually, the following expression is short-circuited.

2. Single Row macro

# Generate a string like "exp" by exp
# @ A generate a character like 'A'
A ## B connects A and B

The first usage is seen here, and the second is not seen yet.
Third, it is useful when A and B are macro parameters. Otherwise, the direct AB will be treated as a thing.

3. Special predefined macros

_ File _ will be replaced with the file in the string format
_ Line _ will be replaced with the row number, unsigned type
_ Date _ is replaced with a date.
_ Time _ will be replaced with the time

In fact, all the books related to c mentioned above should have mentioned these.
However, with a list, there is no practical example,
Of course, I don't know what these things are,
After a long time, you will naturally forget.

4. Comma expression

I feel that it is a rare thing to use,
After all, you can simply use a semicolon when there are multiple sentences.
Although there are many places in the IF and so on which are very compressed, use a comma expression to write several words,
In fact, you can change it without a comma expression.

One is that the priority of the comma expression is very low, so the brackets that follow are really indispensable.

The second is that the value of the comma expression is the rightmost column value.
This is probably something that many people remember, but it doesn't mean much.
Here, it actually makes a difference.
Because the _ asert function is void-type,
If you do not use a comma expression to add 0 to the right,
Reports: (vc6)
Error c2297: '|': illegal, right operand has type 'void'

Adaptation:

You can easily make what you want.
There are also references in asert. h.

For example, the default _ assert pop-up is too depressing...
Just write one by yourself.

# Include <windows. h>
# Include <stdio. h>
# Include <stdlib. h>

# Define max_buffer 200
Void _ assert (char * MSG, char * file, unsigned line ){
Char Buf [max_buffer];
Sprintf (BUF, "assertion fail: \ n % s \ Nin file: \ n % s \ non line: \ n % d", MSG, file, line );
: MessageBox (null, Buf, "assertion failure", mb_ OK );
Exit (0 );
}

# Define assert (exp) | (_ assert (# exp, _ file _, _ line _), 0 ))

Int main (){
Assert (1 = 1 & 3 = 4 );
Return 0;
}

Effect:

Is this even more depressing .. That is not the question discussed here.

By simply changing the _ assert function, you can output relevant information to the file,
Or you can use another method to express it.

As for the assert macro, you can also use it.
The built-in assert is a true expression.
Sometimes assert is a false expression, and a warning is issued when it is true.

For example:

File * fp = fopen ("in.txt", "R ");
If (! FP ){
Exit (0 );
}
Warn (FP = NULL)

Apply the preceding statement. Since it is a real warning, use & replace |.

 

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.