Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><em>[Answering my own question]</em></p> <p>It depends. This is an apples to oranges comparison. Although similar, these macros are NOT interchangeable. Here's a summary of how each works:</p> <p><code>BOOST_STATIC_ASSERT( P )</code> generates a compilation error if <code>P != true</code>.</p> <p><code>BOOST_MPL_ASSERT(( P ))</code> generates a compilation error if <code>P::type::value != true</code>.</p> <p>The latter form, despite <em>requiring double parentheses</em>, is especially useful because it can generate more informative error messages <em>if</em> one uses <em>Boolean nullary Metafunctions</em> from Boost.MPL or TR1's <code>&lt;type_traits&gt;</code> as predicates.</p> <p>Here is an example program that demonstrates how to use (and misuse) these macros:</p> <pre><code>#include &lt;boost/static_assert.hpp&gt; #include &lt;boost/mpl/assert.hpp&gt; #include &lt;type_traits&gt; using namespace ::boost::mpl; using namespace ::std::tr1; struct A {}; struct Z {}; int main() { // boolean predicates BOOST_STATIC_ASSERT( true ); // OK BOOST_STATIC_ASSERT( false ); // assert // BOOST_MPL_ASSERT( false ); // syntax error! // BOOST_MPL_ASSERT(( false )); // syntax error! BOOST_MPL_ASSERT(( bool_&lt; true &gt; )); // OK BOOST_MPL_ASSERT(( bool_&lt; false &gt; )); // assert // metafunction predicates BOOST_STATIC_ASSERT(( is_same&lt; A, A &gt;::type::value ));// OK BOOST_STATIC_ASSERT(( is_same&lt; A, Z &gt;::type::value ));// assert, line 19 BOOST_MPL_ASSERT(( is_same&lt; A, A &gt; )); // OK BOOST_MPL_ASSERT(( is_same&lt; A, Z &gt; )); // assert, line 21 return 0; } </code></pre> <p>For comparison, here are the error messages my compiler (Microsoft Visual C++ 2008) generated for lines 19 and 21 above:</p> <pre><code>1&gt;static_assert.cpp(19) : error C2027: use of undefined type 'boost::STATIC_ASSERTION_FAILURE&lt;x&gt;' 1&gt; with 1&gt; [ 1&gt; x=false 1&gt; ] 1&gt;static_assert.cpp(21) : error C2664: 'boost::mpl::assertion_failed' : cannot convert parameter 1 from 'boost::mpl::failed ************std::tr1::is_same&lt;_Ty1,_Ty2&gt;::* ***********' to 'boost::mpl::assert&lt;false&gt;::type' 1&gt; with 1&gt; [ 1&gt; _Ty1=A, 1&gt; _Ty2=Z 1&gt; ] 1&gt; No constructor could take the source type, or constructor overload resolution was ambiguous </code></pre> <p>So if you're using metafunctions (as defined <a href="http://www.boost.org/doc/libs/1_38_0/libs/mpl/doc/refmanual/metafunction.html" rel="noreferrer">here</a>) as predicates then <code>BOOST_MPL_ASSERT</code> is both less verbose to code and more informative when it asserts.</p> <p>For simple boolean predicates, <code>BOOST_STATIC_ASSERT</code> is less verbose to code although its error messages may be less clear (depending on your compiler.)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload