summaryrefslogtreecommitdiff
path: root/doc/html/boost_staticassert/how.html
blob: 33b29e7a979cd52b3d5926d3cd4d3982b06a2e8d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title> How it works</title>
<link rel="stylesheet" href="../boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.69.1">
<link rel="start" href="../index.html" title="The Boost C++ Libraries">
<link rel="up" href="../boost_staticassert.html" title="Chapter 10. Boost.StaticAssert">
<link rel="prev" href="../boost_staticassert.html" title="Chapter 10. Boost.StaticAssert">
<link rel="next" href="test.html" title=" Test Programs">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%">
<td valign="top"><img alt="boost.png (6897 bytes)" width="277" height="86" src="../../../boost.png"></td>
<td align="center"><a href="../../../index.htm">Home</a></td>
<td align="center"><a href="../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="../../../people/people.htm">People</a></td>
<td align="center"><a href="../../../more/faq.htm">FAQ</a></td>
<td align="center"><a href="../../../more/index.htm">More</a></td>
</table>
<hr>
<div class="spirit-nav">
<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>
</div>
<div class="section" lang="en">
<div class="titlepage"><div><div><h3 class="title">
<a name="boost_staticassert.how"></a> How it works</h3></div></div></div>
<p><code class="computeroutput"><span class="identifier">BOOST_STATIC_ASSERT</span></code> works as follows. There is class <code class="computeroutput"><span class="identifier">STATIC_ASSERTION_FAILURE</span></code>
 which is defined as:</p>
<pre class="programlisting"><code class="literal"><span class="keyword">namespace</span><span class="identifier"> boost</span><span class="special">{</span><span class="keyword">

template</span><span class="special"> &lt;</span><span class="keyword">bool</span><span class="special">&gt;</span><span class="keyword"> struct</span><span class="identifier"> STATIC_ASSERTION_FAILURE</span><span class="special">;</span><span class="keyword">

template</span><span class="special"> &lt;&gt;</span><span class="keyword"> struct</span><span class="identifier"> STATIC_ASSERTION_FAILURE</span><span class="special">&lt;</span><span class="keyword">true</span><span class="special">&gt;{};</span><span class="special">

}</span></code></pre>
<p>
The key feature is that the error message triggered by the undefined 
expression <code class="computeroutput"><span class="keyword">sizeof</span><span class="special">(</span><span class="identifier">STATIC_ASSERTION_FAILURE</span><span class="special">&lt;</span><span class="number">0</span><span class="special">&gt;)</span></code>, tends to be consistent 
across a wide variety of compilers. The rest of the machinery of 
<code class="computeroutput"><span class="identifier">BOOST_STATIC_ASSERT</span></code> is just a way to feed the <code class="computeroutput"><span class="keyword">sizeof</span></code> expression into a <code class="computeroutput"><span class="keyword">typedef</span></code>. 
The use of a macro here is somewhat ugly; however boost members have spent 
considerable effort trying to invent a static assert that avoided macros, 
all to no avail. The general conclusion was that the good of a static assert 
working at namespace, function, and class scope outweighed the ugliness of a macro.</p>
</div>
<table width="100%"><tr>
<td align="left"></td>
<td align="right"><small>Copyright © 2000, 2005 Steve Cleary and John Maddock</small></td>
</tr></table>
<hr>
<div class="spirit-nav">
<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>
</div>
</body>
</html>