1<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 2<html> 3<head> 4<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 5<title>How it works</title> 6<link rel="stylesheet" href="../../../doc/src/boostbook.css" type="text/css"> 7<meta name="generator" content="DocBook XSL Stylesheets V1.79.1"> 8<link rel="home" href="../index.html" title="The Boost C++ Libraries BoostBook Documentation Subset"> 9<link rel="up" href="../boost_staticassert.html" title="Chapter 37. Boost.StaticAssert"> 10<link rel="prev" href="../boost_staticassert.html" title="Chapter 37. Boost.StaticAssert"> 11<link rel="next" href="test.html" title="Test Programs"> 12</head> 13<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> 14<table cellpadding="2" width="100%"><tr> 15<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../boost.png"></td> 16<td align="center"><a href="../../../index.html">Home</a></td> 17<td align="center"><a href="../../../libs/libraries.htm">Libraries</a></td> 18<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> 19<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> 20<td align="center"><a href="../../../more/index.htm">More</a></td> 21</tr></table> 22<hr> 23<div class="spirit-nav"> 24<a accesskey="p" href="../boost_staticassert.html"><img src="../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../boost_staticassert.html"><img src="../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="test.html"><img src="../../../doc/src/images/next.png" alt="Next"></a> 25</div> 26<div class="section"> 27<div class="titlepage"><div><div><h2 class="title" style="clear: both"> 28<a name="boost_staticassert.how"></a>How it works</h2></div></div></div> 29<p> 30 <code class="computeroutput"><span class="identifier">BOOST_STATIC_ASSERT</span></code> works as 31 follows. There is class <code class="computeroutput"><span class="identifier">STATIC_ASSERTION_FAILURE</span></code> 32 which is defined as: 33 </p> 34<pre class="programlisting"><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 xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 56<td align="left"></td> 57<td align="right"><div class="copyright-footer">Copyright © 2000, 2005 Steve Cleary and John Maddock<p> 58 Distributed under the Boost Software License, Version 1.0. (See accompanying 59 file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top"> 60 http://www.boost.org/LICENSE_1_0.txt </a>) 61 </p> 62</div></td> 63</tr></table> 64<hr> 65<div class="spirit-nav"> 66<a accesskey="p" href="../boost_staticassert.html"><img src="../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../boost_staticassert.html"><img src="../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="test.html"><img src="../../../doc/src/images/next.png" alt="Next"></a> 67</div> 68</body> 69</html> 70