C + + Assertion

Source: Internet
Author: User

Assert macros (Basic concepts and usage collation)

In-depth learning of Assert macros

1. Run-time assertion

1.1, assert belongs to the runtime assertion, you can determine whether a given condition is true at run time, if true, do nothing, otherwise print a jump error message, and then terminate the process through abort . When the program is developed, we can add a lot of runtime assertions in debug mode to improve the robustness of our program and to improve the development speed. But when the program needs to be published , a lot of assertions can affect the efficiency of the program, and we only need to add a # define Ndebug before the assert.h header file.

1.2, assert that there is a problem, is bound to abort, forcing the entire program to quit and lead to debugging can not continue, like this, after the problem, we know the problem of the line number, but we need to manually set a breakpoint on the top of the line, Restart debugging to check the status of individual variables when a problem occurs. Moreover, sometimes the problem is not so easy to re- present, so there may be no way to reproduce the error to check the status of the problem.

So, we can write a similar macro ourselves to solve this problem, we want to trigger a breakpoint trap gate interrupt instead of calling abort when the assertion is violated, so that the program pauses when the assertion is violated and waits for the programmer to check the current state for any exceptions

#define _ASSERT (x) if (! ( x)) __asm {int 3}; is to check the assertion and then if the assertion result is False (0), then the inline assembly instruction int 3 is called into a debug interrupt

1.3. User-defined assertions

Implementation function: Variable output can be supported when asserting Smart_assertvalue && "Invalid value!") ("1") (s);

Header file

////////////////////////////////////////////////////////////////////////////////

#include

#include

#include

#include

Class Assert

{

Public

Compile time, Smart_assert macro is replaced, assertion is hit

1. If the assertion is followed by a parameter enclosed in parentheses

Smart_assert_c and Smart_assert_n will not be considered members, but Smart_assert_c (x) and Smart_assert_n (x) macros

Indirect substitution is to call the Showmemoryvalue member method.

2, if the decisive words in the back without brackets, then Smart_assert_c is considered to be a member variable;

If an assertion hit is not followed by multiple macro substitutions, the last one after the SMART_ASSERT_OP macro is replaced. Call is considered a member variable call

Assert & Smart_assert_c;

Assert & Smart_assert_n;//smart_assert_c and Smart_assert_n Loop calls to parse arguments appended to the assertion

Assert (const wchar_t *WEXPR

, const wchar_t *wfile

, const char * expr

, const char * file

, unsigned line)

: _expr (wexpr)

, _file (Wfile)

, _line (line)

, _cfile (file)

, _message (expr)

, Smart_assert_c (*this)

, Smart_assert_n (*this)

{

};

~assert ();

If you need support for printing multiple data types, you can overload the second parameter of the method, or modify the SMART_ASSERT_OP macro, casting when the parameter x is passed in

is a String type

Assert & Restorememoryvalue (const char * key, const std::string &val);

Private

Std::wstring _expr;//Exception expression

std::wstring _file;//file name

Unsigned _line;//file line number

std::string_cfile;//file name

std::string_message;//Exception Message

Std::map _memoryvalue;//Memory Variable Value

};

#ifdef Smart_assert

#undef Smart_assert

#endif

#define SMART_ASSERT_C (x) smart_assert_op (x, N)

#define SMART_ASSERT_N (x) smart_assert_op (x, C)

#define SMART_ASSERT_OP (x, next) \

Smart_assert_c.restorememoryvalue (#x, (x)). smart_assert_# #next

#define SMART_ASSERT (expr) \

if ((expr)); \

Else Assert (_crt_wide (#expr), _crt_wide (__file__), #expr, __file__, __line__). Smart_assert_c

Realize

////////////////////////////////////////////////////////////////////

#include

#include "SmartAssert.h"

Https://msdn.microsoft.com/zh-cn/library/9sb57dw4.aspx: _wassert Description

Assert::~assert ()

{

Get _memoryvalue Temp Variable value print

Enter interrupt

_wassert (_expr.c_str (), _file.c_str (), _line);

}

Assert & Assert::restorememoryvalue (const char *key, const std::string &val)

{

_memoryvalue[key] = val;

return *this;

};

2. Static assertion

In the 2011 C + + Standard, there was a static assertion (Static_assert) syntax, the so-called static assertion, which is the assertion that can be checked at compile time, Static_assert

is the standard syntax for C + + and does not require a reference header file. Another benefit of static assertions is that you can customize the compilation error message when the assertion is violated.

For example:

const int i = 22;

Static_assert (i! =, "I equals to 22");//This code will not compile because the value of I violates the static assertion.

Note: The limitation of a static assertion is that the assertion itself must be a constant expression, and if such i is not a constant, the static assertion is not syntactically compliant.

C + + Assertion

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.