assert.hpp
BOOST_ASSERT
BOOST_ASSERT_MSG
BOOST_VERIFY
BOOST_VERIFY_MSG
BOOST_ASSERT_IS_VOID
BOOST_ASSERT
头文件 <boost/assert.hpp> 定义了宏指令(macro)BOOST_ASSERT,它和<cassert>中定义的标准宏指令类似。
- 默认情况,
BOOST_ASSERT(expr)等同于assert(expr) - 若在引入<boost/assert.hpp>时
BOOST_DISABLE_ASSERTS被定义,BOOST_ASSERT(expr)等于是((void)0),而不管NDEBUG是否已经被定义。这样允许用户在禁用BOOST_ASSERT时不会影响标准assert的定义。 - 若定义了
BOOST_ENABLE_ASSERT_HANDLER当引入<boost/assert.hpp>时,BOOST_ASSERT(expr)等价于:
(BOOST_LIKELY(!!(expr))? ((void)0): ::boost::assertion_failed(#expr, BOOST_CURRENT_FUNCTION, __FILE__, __LINE__))
上面的代码表示判定expr是否为真,若为真,则调用::boost::assertion_failed(#expr, BOOST_CURRENT_FUNCTION, __FILE__, __LINE__)。 这个与NDEBUG是否定义无关。
boost::assertion_failed 在 <boost/assert.hpp> 中定义如下:
namespace boost
{
void assertion_failed(char const * expr, char const * function, char const * file, long line);
}
但是上述从不自己定义,而是由用户需要提供适当的定义。
- 若
BOOST_ENABLE_ASSERT_DEBUG_HANDLER定义时也引入了<boost/assert.hpp>,在NDEBUG也定义时,BOOST_ASSERT(expr)等价于((void)0)。
与<cassert>一样,<boost/assert.hpp>可以在单个translation unit中包含多次。每次按上述规定重新定义boost断言
BOOST_ASSERT_MSG
BOOST_ASSERT_MSG 和BOOST_ASSERT类似,但多了一个额外的字符串字面值,用来显示错误信息。
BOOST_ASSERT_MSG默认是assert((expr)&&(msg))等价于assert((expr)&&(msg))。- 当引入
<boost/assert.hpp>时若BOOST_DISABLE_ASSERTS被定义了,则无论宏ndebug是否定义,BOOST_ASSERT_MSG(expr,msg)都相当于((void)0), - 当引入
<boost/assert.hpp>时若BOOST_ENABLE_ASSERT_HANDLER被定义了,BOOST_ASSERT_MSG(expr,msg)等价于:
(BOOST_LIKELY(!!(expr))? ((void)0): ::boost::assertion_failed_msg(#expr, msg, BOOST_CURRENT_FUNCTION, __FILE__, __LINE__))
此时,与NDEBUG 是否定义无关。
<boost/assert.hpp>中boost::assertion_failed_msg定义如下:
namespace boost
{
void assertion_failed_msg(char const * expr, char const * msg, char const * function, char const * file, long line);
}
but it is never defined. The user is expected to supply an appropriate definition.
- 当引入<boost/assert.hpp>时若 定义了
BOOST_ENABLE_ASSERT_DEBUG_HANDLER,BOOST_ASSERT_MSG(expr)在NDEBUG定义时就相当于((void)0)
和<cassert>一样, <boost/assert.hpp> 也可以被引入(included)多次在a single translation unit.
boost_assert_msg将按上述规定每次重新定义。
BOOST_VERIFY
宏boost_verify与boost_assert具有相同的行为,只是传递给boost_verify的表达式总是被计算出来的。当断言表达式具有理想的副作用时,这是有用的;当变量的唯一使用是在断言中时,它还可以帮助抑制有关未使用变量的警告。
- 当引入<boost/assert.hpp>时若定义了
BOOST_DISABLE_ASSERTS,BOOST_VERIFY(expr)expands to((void)(expr)). - 当引入<boost/assert.hpp>时若定义了
BOOST_ENABLE_ASSERT_HANDLER,BOOST_VERIFY(expr)等价于BOOST_ASSERT(expr). - 否则,当
NDEBUG定义时,BOOST_VERIFY(expr)相当于((void)(expr));NDEBUG未定义时,它相当于BOOST_ASSERT(expr)。
BOOST_VERIFY_MSG
BOOST_VERIFY_MSG类似于 BOOST_VERIFY,也只是多了一个错误提示信息的参数。
- 当引入<boost/assert.hpp>时若 定义了
BOOST_DISABLE_ASSERTS,BOOST_VERIFY_MSG(expr,msg)等价于((void)(expr)). - 当引入<boost/assert.hpp>时若 定义了
BOOST_ENABLE_ASSERT_HANDLER,BOOST_VERIFY_MSG(expr,msg)等价于BOOST_ASSERT_MSG(expr,msg). - 否则, 当定义了
NDEBUG时BOOST_VERIFY_MSG(expr,msg)等价于((void)(expr));若未定义NDEBUG,它相当于BOOST_ASSERT_MSG(expr,msg)。
BOOST_ASSERT_IS_VOID
宏boost_assert_is_void是在boost_assert和boost_assert_msg被定义时,等价于((void)0)。此宏有助于避免编译和可能运行仅用于准备断言中使用的数据的代码。
void MyContainer::erase(iterator i)
{
//Some sanity checks, data must be ordered
#ifndef BOOST_ASSERT_IS_VOID
if(i != c.begin()){
iterator prev = i;
--prev;
BOOST_ASSERT(*prev < *i);
}
else if(i != c.end()){
iterator next = i;
++next;
BOOST_ASSERT(*i < *next);
}
#endif
this->erase_impl(i);
}
- 默认情况下,如果定义了ndebug,则会定义boost_assert_is_void。
- 如果宏boost_disable_断言已定义,则始终定义boost_assert_is_void。
- 如果宏boost_enable_assert_handler已定义,则不会定义boost_assert_is_void。
- 如果宏boost_enable_assert_debug处理程序,则在定义ndebug时定义boost_assert_is_void。
参考:
【1】 https://www.boost.org/doc/libs/1_61_0/libs/assert/assert.html#BOOST_ASSERT_MSG
115



被折叠的 条评论
为什么被折叠?



