boost诊断工具BOOST_ASSERT、BOOST_VERIFY、BOOST_STATIC_ASSERT

boost.assert提供的主要工具是BOOST_ASSERT宏,类似于C语言的assert,提供运行时的断言,但功能有所增强;
默认情况下,BOOST_ASSERT宏等同于assert宏: # define BOOST_ASSERT(expr) assert(expr);
BOOST_ASSERT宏仅会在Debug模式下起作用,在Release模式下不会被编译,不会影响运行效率;
BOOST_ASSERT宏是标准断言宏assert的增强版本,使用更加灵活;
定义BOOST_DISABLE_ASSERTS宏可禁止BOOST_ASSERT作用,但assert宏不会受影响;
定义BOOST_ENABLE_ASSERT_HANDLER宏将导致BOOST_ASSERT的行为发生改变;

BOOST_VERIFY宏是assert库提供的另一种工具,断言表达式一定会被求值,其余与BOOST_ASSERT行为相同。

assert与BOOST_ASSERT是运行时断言,但有时候运行时已经很晚了,程序已经发生了无可挽回的错误;
static_assert库能够把断言诊断的时刻由运行时提前到编译期,增加程序的健壮性;
BOOST_STATIC_ASSERT是一个编译期断言,使用typedef和模板元技术实现;
BOOST_STATIC_ASSERT可以出现在程序的任何位置:命名空间中、类中、函数中.

 C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
 
/*
    boost.assert提供的主要工具是BOOST_ASSERT宏,类似于C语言的assert,提供运行时的断言,但功能有所增强;
    默认情况下,BOOST_ASSERT宏等同于assert宏: # define BOOST_ASSERT(expr) assert(expr);
    BOOST_ASSERT宏仅会在Debug模式下起作用,在Release模式下不会被编译,不会影响运行效率;
    BOOST_ASSERT宏是标准断言宏assert的增强版本,使用更加灵活;
    定义BOOST_DISABLE_ASSERTS宏可禁止BOOST_ASSERT作用,但assert宏不会受影响;
    定义BOOST_ENABLE_ASSERT_HANDLER宏将导致BOOST_ASSERT的行为发生改变;
    
    BOOST_VERIFY宏是assert库提供的另一种工具,断言表达式一定会被求值,其余与BOOST_ASSERT行为相同。

    assert与BOOST_ASSERT是运行时断言,但有时候运行时已经很晚了,程序已经发生了无可挽回的错误;
    static_assert库能够把断言诊断的时刻由运行时提前到编译期,增加程序的健壮性;
    BOOST_STATIC_ASSERT是一个编译期断言,使用typedef和模板元技术实现;
    BOOST_STATIC_ASSERT可以出现在程序的任何位置:命名空间中、类中、函数中.
*/



/************************************************************************/
/* C++ stl Library                                                        */
/************************************************************************/
#include <iostream>
#include <string>

/************************************************************************/
/* C++ boost Library                                                   */
/************************************************************************/
#define BOOST_DISABLE_ASSERTS               //禁用BOOST_ASSERT
#define BOOST_ENABLE_ASSERT_HANDLER         //为BOOST_ASSERT添加handler
#include <boost/assert.hpp>
#include <boost/format.hpp>
#include <boost/static_assert.hpp>
#include <cassert>
using namespace std;

namespace boost
{
    
void assertion_failed(char const * expr, char const * function, char const * file, long line)
    {
        boost::format fmt(
"Assertion Failed! Expression: %s Function: %s File: %s Line: %ld ");
        fmt % expr% function% file% line;
        cout << fmt;
    }
}

template<typename T>
void print(T &tok)
{
    
for(BOOST_AUTO(pos, tok.begin()); pos != tok.end(); pos++)
    {
        cout << 
"[" << *pos << "]" ;
    }
    cout << endl;
}

double func(int x)
{
    BOOST_ASSERT(x != 
0 && "divided by zero!");
    
return 1.0/x;
}

int main(void)
{
    BOOST_ASSERT(
16 == 0x10);
    
//assert(16 == 0x11);
    //BOOST_ASSERT(string().size() == 1);
    //func(0);

    
int nLen = 0;
    string str(
"Michael Joessy!");
    BOOST_VERIFY(nLen = str.length());
    cout << 
"length of str is " << nLen << endl;

    BOOST_STATIC_ASSERT(
2 == sizeof(short));
    
//BOOST_STATIC_ASSERT(3 == sizeof(short));

    cin.get();
    
return 0;
}
原文地址:https://www.cnblogs.com/MakeView660/p/7126916.html