| 1 | <html> |
|---|
| 2 | <head> |
|---|
| 3 | <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> |
|---|
| 4 | <title> How it works</title> |
|---|
| 5 | <link rel="stylesheet" href="../boostbook.css" type="text/css"> |
|---|
| 6 | <meta name="generator" content="DocBook XSL Stylesheets V1.68.1"> |
|---|
| 7 | <link rel="start" href="../index.html" title="The Boost C++ Libraries BoostBook Documentation Subset"> |
|---|
| 8 | <link rel="up" href="../boost_staticassert.html" title="Chapter 13. Boost.StaticAssert"> |
|---|
| 9 | <link rel="prev" href="../boost_staticassert.html" title="Chapter 13. Boost.StaticAssert"> |
|---|
| 10 | <link rel="next" href="test.html" title=" Test Programs"> |
|---|
| 11 | </head> |
|---|
| 12 | <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> |
|---|
| 13 | <table cellpadding="2" width="100%"> |
|---|
| 14 | <td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../boost.png"></td> |
|---|
| 15 | <td align="center"><a href="../../../index.htm">Home</a></td> |
|---|
| 16 | <td align="center"><a href="../../../libs/libraries.htm">Libraries</a></td> |
|---|
| 17 | <td align="center"><a href="../../../people/people.htm">People</a></td> |
|---|
| 18 | <td align="center"><a href="../../../more/faq.htm">FAQ</a></td> |
|---|
| 19 | <td align="center"><a href="../../../more/index.htm">More</a></td> |
|---|
| 20 | </table> |
|---|
| 21 | <hr> |
|---|
| 22 | <div class="spirit-nav"> |
|---|
| 23 | <a accesskey="p" href="../boost_staticassert.html"><img src="../images/prev.png" alt="Prev"></a><a accesskey="u" href="../boost_staticassert.html"><img src="../images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../images/home.png" alt="Home"></a><a accesskey="n" href="test.html"><img src="../images/next.png" alt="Next"></a> |
|---|
| 24 | </div> |
|---|
| 25 | <div class="section" lang="en"> |
|---|
| 26 | <div class="titlepage"><div><div><h2 class="title" style="clear: both"> |
|---|
| 27 | <a name="boost_staticassert.how"></a> How it works</h2></div></div></div> |
|---|
| 28 | <p> |
|---|
| 29 | <code class="computeroutput"><span class="identifier">BOOST_STATIC_ASSERT</span></code> works as |
|---|
| 30 | follows. There is class <code class="computeroutput"><span class="identifier">STATIC_ASSERTION_FAILURE</span></code> |
|---|
| 31 | which is defined as: |
|---|
| 32 | </p> |
|---|
| 33 | <pre class="programlisting"> |
|---|
| 34 | <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">{</span> |
|---|
| 35 | |
|---|
| 36 | <span class="keyword">template</span> <span class="special"><</span><span class="keyword">bool</span><span class="special">></span> <span class="keyword">struct</span> <span class="identifier">STATIC_ASSERTION_FAILURE</span><span class="special">;</span> |
|---|
| 37 | |
|---|
| 38 | <span class="keyword">template</span> <span class="special"><></span> <span class="keyword">struct</span> <span class="identifier">STATIC_ASSERTION_FAILURE</span><span class="special"><</span><span class="keyword">true</span><span class="special">>{};</span> |
|---|
| 39 | |
|---|
| 40 | <span class="special">}</span> |
|---|
| 41 | </pre> |
|---|
| 42 | <p> |
|---|
| 43 | The key feature is that the error message triggered by the undefined expression |
|---|
| 44 | <code class="computeroutput"><span class="keyword">sizeof</span><span class="special">(</span><span class="identifier">STATIC_ASSERTION_FAILURE</span><span class="special"><</span><span class="number">0</span><span class="special">>)</span></code>, tends |
|---|
| 45 | to be consistent across a wide variety of compilers. The rest of the machinery |
|---|
| 46 | of <code class="computeroutput"><span class="identifier">BOOST_STATIC_ASSERT</span></code> is just |
|---|
| 47 | a way to feed the <code class="computeroutput"><span class="keyword">sizeof</span></code> expression |
|---|
| 48 | into a <code class="computeroutput"><span class="keyword">typedef</span></code>. The use of a macro |
|---|
| 49 | here is somewhat ugly; however boost members have spent considerable effort |
|---|
| 50 | trying to invent a static assert that avoided macros, all to no avail. The |
|---|
| 51 | general conclusion was that the good of a static assert working at namespace, |
|---|
| 52 | function, and class scope outweighed the ugliness of a macro. |
|---|
| 53 | </p> |
|---|
| 54 | </div> |
|---|
| 55 | <table width="100%"><tr> |
|---|
| 56 | <td align="left"></td> |
|---|
| 57 | <td align="right"><small>Copyright © 2000, 2005 Steve Cleary and John Maddock</small></td> |
|---|
| 58 | </tr></table> |
|---|
| 59 | <hr> |
|---|
| 60 | <div class="spirit-nav"> |
|---|
| 61 | <a accesskey="p" href="../boost_staticassert.html"><img src="../images/prev.png" alt="Prev"></a><a accesskey="u" href="../boost_staticassert.html"><img src="../images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../images/home.png" alt="Home"></a><a accesskey="n" href="test.html"><img src="../images/next.png" alt="Next"></a> |
|---|
| 62 | </div> |
|---|
| 63 | </body> |
|---|
| 64 | </html> |
|---|