Add BOOST_VERIFY_MSG; change BOOST_ASSERT_MSG(x,m) to assert((x)&&(m)) for consistency with BOOST_ASSERT; add more tests

This commit is contained in:
Peter Dimov
2014-02-12 02:05:27 +02:00
parent a2da83b260
commit f0e5bd15fb
9 changed files with 716 additions and 112 deletions

View File

@@ -2,12 +2,12 @@
<html>
<head>
<title>Boost: assert.hpp documentation</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<body bgcolor="white" style="MARGIN-LEFT: 5%; MARGIN-RIGHT: 5%">
<table border="0" width="100%">
<tr>
<td width="277"><A href="../../index.htm"> <img src="../../boost.png" alt="boost.png (6897 bytes)" width="277" height="86" border="0"></A>
<td width="277"><a href="../../index.htm"> <img src="../../boost.png" alt="boost.png (6897 bytes)" width="277" height="86" border="0" /></a>
</td>
<td align="center">
<h1>assert.hpp</h1>
@@ -18,31 +18,32 @@
</tr>
</table>
<p>
<a href="#BOOST_ASSERT">BOOST_ASSERT</a><br>
<a href="#BOOST_ASSERT_MSG">BOOST_ASSERT_MSG</a><br>
<a href="#BOOST_VERIFY">BOOST_VERIFY</a></p>
<a href="#BOOST_ASSERT">BOOST_ASSERT</a><br />
<a href="#BOOST_ASSERT_MSG">BOOST_ASSERT_MSG</a><br />
<a href="#BOOST_VERIFY">BOOST_VERIFY</a><br />
<a href="#BOOST_VERIFY_MSG">BOOST_VERIFY_MSG</a><br />
</p>
<h2><a name="BOOST_ASSERT">BOOST_ASSERT</a></h2>
<p>
The header <STRONG>&lt;boost/assert.hpp&gt;</STRONG> defines the macro <b>BOOST_ASSERT</b>,
which is similar to the standard <STRONG>assert</STRONG> macro defined in <STRONG>&lt;cassert&gt;</STRONG>.
The header <code>&lt;boost/assert.hpp&gt;</code> defines the macro <code>BOOST_ASSERT</code>,
which is similar to the standard <code>assert</code> macro defined in <code>&lt;cassert&gt;</code>.
The macro is intended to be used in both Boost libraries and user
code.
</p>
<P>By default, <tt>BOOST_ASSERT(expr)</tt> is equivalent to <tt>assert(expr)</tt>.</P>
<P>If the macro <STRONG>BOOST_DISABLE_ASSERTS</STRONG> is defined when <STRONG>&lt;boost/assert.hpp&gt;</STRONG>
is included, <tt>BOOST_ASSERT(expr)</tt> is defined as <tt>((void)0)</tt>. This
allows users to selectively disable <STRONG>BOOST_ASSERT</STRONG> without
affecting the definition of the standard <STRONG>assert</STRONG>.</P>
<P>If the macro <STRONG>BOOST_ENABLE_ASSERT_HANDLER</STRONG> is defined when <STRONG>&lt;boost/assert.hpp&gt;</STRONG>
is included, <tt>BOOST_ASSERT(expr)</tt> evaluates <b>expr</b> and, if the
result is false, evaluates the expression</P>
<p>&bull; By default, <code>BOOST_ASSERT(expr)</code> expands to <code>assert(expr)</code>.</p>
<p>&bull; If the macro <code>BOOST_DISABLE_ASSERTS</code> is defined when <code>&lt;boost/assert.hpp&gt;</code>
is included, <code>BOOST_ASSERT(expr)</code> expands to <code>((void)0)</code>, regardless of whether
the macro <code>NDEBUG</code> is defined. This allows users to selectively disable <code>BOOST_ASSERT</code> without
affecting the definition of the standard <code>assert</code>.</p>
<p>&bull; If the macro <code>BOOST_ENABLE_ASSERT_HANDLER</code> is defined when <code>&lt;boost/assert.hpp&gt;</code>
is included, <code>BOOST_ASSERT(expr)</code> expands to</p>
<blockquote>
<P><tt>::boost::assertion_failed(#expr, <a href="current_function.html">BOOST_CURRENT_FUNCTION</a>,
__FILE__, __LINE__)</tt></P>
</blockquote>
<P><STRONG>assertion_failed</STRONG> is declared in <STRONG>&lt;boost/assert.hpp&gt;</STRONG>
as</P>
<pre>(<a href="../config/doc/html/boost_config/boost_macro_reference.html#boost_config.boost_macro_reference.boost_helper_macros">BOOST_LIKELY</a>(!!(expr))? ((void)0): ::boost::assertion_failed(#expr, <a href="current_function.html">BOOST_CURRENT_FUNCTION</a>, __FILE__, __LINE__))</pre>
</blockquote>
<p>That is, it evaluates <code>expr</code> and if it's false, calls <code>::boost::assertion_failed(#expr, BOOST_CURRENT_FUNCTION, __FILE__, __LINE__)</code>.
This is true regardless of whether <code>NDEBUG</code> is defined.</p>
<p><code>boost::assertion_failed</code> is declared in <code>&lt;boost/assert.hpp&gt;</code> as</p>
<blockquote>
<pre>namespace boost
{
@@ -52,40 +53,28 @@
</blockquote>
<p>but it is never defined. The user is expected to supply an appropriate
definition.</p>
<P>As is the case with <STRONG>&lt;cassert&gt;</STRONG>, <STRONG>&lt;boost/assert.hpp&gt;</STRONG>
can be included multiple times in a single translation unit. <STRONG>BOOST_ASSERT</STRONG>
will be redefined each time as specified above.</P>
<p>&bull; If the macro <code>BOOST_ENABLE_ASSERT_DEBUG_HANDLER</code> is defined when <code>&lt;boost/assert.hpp&gt;</code>
is included, <code>BOOST_ASSERT(expr)</code> expands to <code>((void)0)</code> when <code>NDEBUG</code> is
defined. Otherwise the behavior is as if <code>BOOST_ENABLE_ASSERT_HANDLER</code> has been defined.</p>
<p>As is the case with <code>&lt;cassert&gt;</code>, <code>&lt;boost/assert.hpp&gt;</code>
can be included multiple times in a single translation unit. <code>BOOST_ASSERT</code>
will be redefined each time as specified above.</p>
<h2><a name="BOOST_ASSERT_MSG">BOOST_ASSERT_MSG</a></h2>
<p>
The header <STRONG>&lt;boost/assert.hpp&gt;</STRONG> defines the macro <b>BOOST_ASSERT_MSG</b>,
which is similar to the standard <STRONG>assert</STRONG> macro defined in <STRONG>&lt;cassert&gt;</STRONG>,
but with an additional macro parameter supplying an error message. The macro is intended to be used in both Boost libraries
and user code.
</p>
<P> <tt>BOOST_ASSERT_MSG(expr, msg)</tt> is equivalent to <code>
((void)0)</code> if <b>BOOST_DISABLE_ASSERTS</b> or <b>NDEBUG</b> are
defined or <code>expr</code> evaluates to <code>true</code>. If those
macros and <STRONG>BOOST_ENABLE_ASSERT_HANDLER</STRONG> are not
defined, and <code>expr</code> evaluates to <code>false</code>, an error
message that includes <tt>#expr</tt>, <tt>msg</tt>, <tt> <a href="current_function.html">BOOST_CURRENT_FUNCTION</a></tt>, <tt>
__FILE__</tt>, and <tt>__LINE__</tt> is sent to output stream <b>
BOOST_ASSERT_MSG_OSTREAM</b>
and <code>std::abort()</code> is called.</P>
<P> <b>BOOST_ASSERT_MSG_OSTREAM</b> defines the output stream. It defaults to <code>std::cerr</code>.
Integrated development environments (IDE's) like Microsoft Visual Studio
may produce easier to understand output if messages go to a different
stream, such as <code>std::cout</code>. Users may define <b>BOOST_ASSERT_MSG_OSTREAM</b> before including <STRONG>&lt;boost/assert.hpp&gt;</STRONG>
to specify a different output stream.&nbsp; </P>
<P>If the macro <STRONG>BOOST_ENABLE_ASSERT_HANDLER</STRONG> is defined when <STRONG>&lt;boost/assert.hpp&gt;</STRONG>
is included, instead of sending a error message to an output
stream, this expression is evaluated</P>
The macro <code>BOOST_ASSERT_MSG</code> is similar to <code>BOOST_ASSERT</code>, but it takes an additional argument,
a character literal, supplying an error message.</p>
<p>&bull; By default, <code>BOOST_ASSERT_MSG(expr,msg)</code> expands to <code>assert((expr)&amp;&amp;(msg))</code>.</p>
<p>&bull; If the macro <code>BOOST_DISABLE_ASSERTS</code> is defined when <code>&lt;boost/assert.hpp&gt;</code>
is included, <code>BOOST_ASSERT_MSG(expr,msg)</code> expands to <code>((void)0)</code>, regardless of whether
the macro <code>NDEBUG</code> is defined.</p>
<p>&bull; If the macro <code>BOOST_ENABLE_ASSERT_HANDLER</code> is defined when <code>&lt;boost/assert.hpp&gt;</code>
is included, <code>BOOST_ASSERT_MSG(expr,msg)</code> expands to</p>
<blockquote>
<P><tt>::boost::assertion_failed_msg(#expr, msg, <a href="current_function.html">BOOST_CURRENT_FUNCTION</a>,
__FILE__, __LINE__)</tt></P>
</blockquote>
<P><STRONG>assertion_failed_msg</STRONG> is declared in <STRONG>&lt;boost/assert.hpp&gt;</STRONG>
as</P>
<pre>(<a href="../config/doc/html/boost_config/boost_macro_reference.html#boost_config.boost_macro_reference.boost_helper_macros">BOOST_LIKELY</a>(!!(expr))? ((void)0): ::boost::assertion_failed_msg(#expr, msg, <a href="current_function.html">BOOST_CURRENT_FUNCTION</a>, __FILE__, __LINE__))</pre>
</blockquote>
<p>This is true regardless of whether <code>NDEBUG</code> is defined.</p>
<p><code>boost::assertion_failed_msg</code> is declared in <code>&lt;boost/assert.hpp&gt;</code> as</p>
<blockquote>
<pre>namespace boost
{
@@ -95,21 +84,38 @@
</blockquote>
<p>but it is never defined. The user is expected to supply an appropriate
definition.</p>
<P>As is the case with <STRONG>&lt;cassert&gt;</STRONG>, <STRONG>&lt;boost/assert.hpp&gt;</STRONG>
can be included multiple times in a single translation unit. <STRONG>BOOST_ASSERT_MSG</STRONG>
will be redefined each time as specified above.</P>
<p>&bull; If the macro <code>BOOST_ENABLE_ASSERT_DEBUG_HANDLER</code> is defined when <code>&lt;boost/assert.hpp&gt;</code>
is included, <code>BOOST_ASSERT_MSG(expr)</code> expands to <code>((void)0)</code> when <code>NDEBUG</code> is
defined. Otherwise the behavior is as if <code>BOOST_ENABLE_ASSERT_HANDLER</code> has been defined.</p>
<p>As is the case with <code>&lt;cassert&gt;</code>, <code>&lt;boost/assert.hpp&gt;</code>
can be included multiple times in a single translation unit. <code>BOOST_ASSERT_MSG</code>
will be redefined each time as specified above.</p>
<h2><a name="BOOST_VERIFY">BOOST_VERIFY</a></h2>
<p><STRONG>&lt;boost/assert.hpp&gt;</STRONG> also defines the macro <STRONG>BOOST_VERIFY</STRONG>.
It has exactly the same behavior as <STRONG>BOOST_ASSERT</STRONG>, except that
the expression that is passed to <STRONG>BOOST_VERIFY</STRONG> is always
<p>The macro <code>BOOST_VERIFY</code> has the same behavior as <code>BOOST_ASSERT</code>, except that
the expression that is passed to <code>BOOST_VERIFY</code> is always
evaluated. This is useful when the asserted expression has desirable side
effects; it can also help suppress warnings about unused variables when the
only use of the variable is inside an assertion.</p>
<p><br>
<small>Copyright © 2002, 2007 by Peter Dimov.&nbsp; Copyright © 2011
<p>&bull; If the macro <code>BOOST_DISABLE_ASSERTS</code> is defined when <code>&lt;boost/assert.hpp&gt;</code>
is included, <code>BOOST_VERIFY(expr)</code> expands to <code>((void)(expr))</code>.</p>
<p>&bull; If the macro <code>BOOST_ENABLE_ASSERT_HANDLER</code> is defined when <code>&lt;boost/assert.hpp&gt;</code>
is included, <code>BOOST_VERIFY(expr)</code> expands to <code>BOOST_ASSERT(expr)</code>.</p>
<p>&bull; Otherwise, <code>BOOST_VERIFY(expr)</code> expands to <code>((void)(expr))</code> when <code>NDEBUG</code> is
defined, to <code>BOOST_ASSERT(expr)</code> when it's not.</p>
<h2><a name="BOOST_VERIFY_MSG">BOOST_VERIFY_MSG</a></h2>
<p>The macro <code>BOOST_VERIFY_MSG</code> is similar to <code>BOOST_VERIFY</code>, with an additional parameter, an error message.</p>
<p>&bull; If the macro <code>BOOST_DISABLE_ASSERTS</code> is defined when <code>&lt;boost/assert.hpp&gt;</code>
is included, <code>BOOST_VERIFY_MSG(expr,msg)</code> expands to <code>((void)(expr))</code>.</p>
<p>&bull; If the macro <code>BOOST_ENABLE_ASSERT_HANDLER</code> is defined when <code>&lt;boost/assert.hpp&gt;</code>
is included, <code>BOOST_VERIFY_MSG(expr,msg)</code> expands to <code>BOOST_ASSERT_MSG(expr,msg)</code>.</p>
<p>&bull; Otherwise, <code>BOOST_VERIFY_MSG(expr,msg)</code> expands to <code>((void)(expr))</code> when <code>NDEBUG</code> is
defined, to <code>BOOST_ASSERT_MSG(expr,msg)</code> when it's not.</p>
<hr />
<p>
<small>Copyright © 2002, 2007, 2014 by Peter Dimov.&nbsp; Copyright © 2011
by Beman Dawes. Distributed under the Boost Software
License, Version 1.0. See accompanying file <A href="../../LICENSE_1_0.txt">LICENSE_1_0.txt</A>
or copy at <A href="http://www.boost.org/LICENSE_1_0.txt">http://www.boost.org/LICENSE_1_0.txt</A>.</small></p>
License, Version 1.0. See accompanying file <a href="../../LICENSE_1_0.txt">LICENSE_1_0.txt</a>
or copy at <a href="http://www.boost.org/LICENSE_1_0.txt">http://www.boost.org/LICENSE_1_0.txt</a>.</small></p>
</body>
</html>
</html>