1<html> 2<head> 3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 4<title>Compile time min/max calculation</title> 5<link rel="stylesheet" href="../../../../../doc/src/boostbook.css" type="text/css"> 6<meta name="generator" content="DocBook XSL Stylesheets V1.79.1"> 7<link rel="home" href="../index.html" title="Boost.Integer"> 8<link rel="up" href="../index.html" title="Boost.Integer"> 9<link rel="prev" href="log2.html" title="Compile Time log2 Calculation"> 10<link rel="next" href="history.html" title="History"> 11</head> 12<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> 13<table cellpadding="2" width="100%"><tr> 14<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../boost.png"></td> 15<td align="center"><a href="../../../../../index.html">Home</a></td> 16<td align="center"><a href="../../../../../libs/libraries.htm">Libraries</a></td> 17<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> 18<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> 19<td align="center"><a href="../../../../../more/index.htm">More</a></td> 20</tr></table> 21<hr> 22<div class="spirit-nav"> 23<a accesskey="p" href="log2.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.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="history.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a> 24</div> 25<div class="section"> 26<div class="titlepage"><div><div><h2 class="title" style="clear: both"> 27<a name="boost_integer.minmax"></a><a class="link" href="minmax.html" title="Compile time min/max calculation">Compile time min/max calculation</a> 28</h2></div></div></div> 29<div class="toc"><dl class="toc"> 30<dt><span class="section"><a href="minmax.html#boost_integer.minmax.synopsis">Synopsis</a></span></dt> 31<dt><span class="section"><a href="minmax.html#boost_integer.minmax.usage">Usage</a></span></dt> 32<dt><span class="section"><a href="minmax.html#boost_integer.minmax.example">Example</a></span></dt> 33<dt><span class="section"><a href="minmax.html#boost_integer.minmax.demonstration_program">Demonstration 34 Program</a></span></dt> 35<dt><span class="section"><a href="minmax.html#boost_integer.minmax.rationale">Rationale</a></span></dt> 36<dt><span class="section"><a href="minmax.html#boost_integer.minmax.credits">Credits</a></span></dt> 37</dl></div> 38<p> 39 The class templates in <a href="../../../../../boost/integer/static_min_max.hpp" target="_top"><boost/integer/static_min_max.hpp></a> 40 provide a compile-time evaluation of the minimum or maximum of two integers. 41 These facilities are useful for generic programming problems. 42 </p> 43<div class="section"> 44<div class="titlepage"><div><div><h3 class="title"> 45<a name="boost_integer.minmax.synopsis"></a><a class="link" href="minmax.html#boost_integer.minmax.synopsis" title="Synopsis">Synopsis</a> 46</h3></div></div></div> 47<pre class="programlisting"><span class="keyword">namespace</span> <span class="identifier">boost</span> 48<span class="special">{</span> 49 50<span class="keyword">typedef</span> <span class="emphasis"><em>implementation-defined</em></span> <span class="identifier">static_min_max_signed_type</span><span class="special">;</span> 51<span class="keyword">typedef</span> <span class="emphasis"><em>implementation-defined</em></span> <span class="identifier">static_min_max_unsigned_type</span><span class="special">;</span> 52 53<span class="keyword">template</span> <span class="special"><</span><span class="identifier">static_min_max_signed_type</span> <span class="identifier">Value1</span><span class="special">,</span> <span class="identifier">static_min_max_signed_type</span> <span class="identifier">Value2</span> <span class="special">></span> 54 <span class="keyword">struct</span> <span class="identifier">static_signed_min</span><span class="special">;</span> 55 56<span class="keyword">template</span> <span class="special"><</span><span class="identifier">static_min_max_signed_type</span> <span class="identifier">Value1</span><span class="special">,</span> <span class="identifier">static_min_max_signed_type</span> <span class="identifier">Value2</span><span class="special">></span> 57 <span class="keyword">struct</span> <span class="identifier">static_signed_max</span><span class="special">;</span> 58 59<span class="keyword">template</span> <span class="special"><</span><span class="identifier">static_min_max_unsigned_type</span> <span class="identifier">Value1</span><span class="special">,</span> <span class="identifier">static_min_max_unsigned_type</span> <span class="identifier">Value2</span><span class="special">></span> 60 <span class="keyword">struct</span> <span class="identifier">static_unsigned_min</span><span class="special">;</span> 61 62<span class="keyword">template</span> <span class="special"><</span><span class="identifier">static_min_max_unsigned_type</span> <span class="identifier">Value1</span><span class="special">,</span> <span class="identifier">static_min_max_unsigned_type</span> <span class="identifier">Value2</span><span class="special">></span> 63 <span class="keyword">struct</span> <span class="identifier">static_unsigned_max</span><span class="special">;</span> 64 65<span class="special">}</span> 66</pre> 67</div> 68<div class="section"> 69<div class="titlepage"><div><div><h3 class="title"> 70<a name="boost_integer.minmax.usage"></a><a class="link" href="minmax.html#boost_integer.minmax.usage" title="Usage">Usage</a> 71</h3></div></div></div> 72<p> 73 The four class templates provide the combinations for finding the minimum 74 or maximum of two <code class="literal">signed</code> or <code class="literal">unsigned</code> 75 (<code class="literal">long</code>) parameters, <span class="emphasis"><em>Value1</em></span> and <span class="emphasis"><em>Value2</em></span>, 76 at compile-time. Each template has a single static data member, <code class="literal">value</code>, 77 which is set to the respective minimum or maximum of the template's parameters. 78 </p> 79</div> 80<div class="section"> 81<div class="titlepage"><div><div><h3 class="title"> 82<a name="boost_integer.minmax.example"></a><a class="link" href="minmax.html#boost_integer.minmax.example" title="Example">Example</a> 83</h3></div></div></div> 84<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">integer</span><span class="special">/</span><span class="identifier">static_min_max</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span> 85 86<span class="keyword">template</span> <span class="special"><</span> <span class="keyword">unsigned</span> <span class="keyword">long</span> <span class="identifier">AddendSize1</span><span class="special">,</span> <span class="keyword">unsigned</span> <span class="keyword">long</span> <span class="identifier">AddendSize2</span> <span class="special">></span> 87<span class="keyword">class</span> <span class="identifier">adder</span> 88<span class="special">{</span> 89<span class="keyword">public</span><span class="special">:</span> 90 <span class="keyword">static</span> <span class="keyword">unsigned</span> <span class="keyword">long</span> <span class="keyword">const</span> <span class="identifier">addend1_size</span> <span class="special">=</span> <span class="identifier">AddendSize1</span><span class="special">;</span> 91 <span class="keyword">static</span> <span class="keyword">unsigned</span> <span class="keyword">long</span> <span class="keyword">const</span> <span class="identifier">addend2_size</span> <span class="special">=</span> <span class="identifier">AddendSize2</span><span class="special">;</span> 92 <span class="keyword">static</span> <span class="keyword">unsigned</span> <span class="keyword">long</span> <span class="keyword">const</span> <span class="identifier">sum_size</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">static_unsigned_max</span><span class="special"><</span><span class="identifier">AddendSize1</span><span class="special">,</span> <span class="identifier">AddendSize2</span><span class="special">>::</span><span class="identifier">value</span> <span class="special">+</span> <span class="number">1</span><span class="special">;</span> 93 94 <span class="keyword">typedef</span> <span class="keyword">int</span> <span class="identifier">addend1_type</span><span class="special">[</span> <span class="identifier">addend1_size</span> <span class="special">];</span> 95 <span class="keyword">typedef</span> <span class="keyword">int</span> <span class="identifier">addend2_type</span><span class="special">[</span> <span class="identifier">addend2_size</span> <span class="special">];</span> 96 <span class="keyword">typedef</span> <span class="keyword">int</span> <span class="identifier">sum_type</span><span class="special">[</span> <span class="identifier">sum_size</span> <span class="special">];</span> 97 98 <span class="keyword">void</span> <span class="keyword">operator</span> <span class="special">()(</span> <span class="identifier">addend1_type</span> <span class="keyword">const</span> <span class="special">&</span><span class="identifier">a1</span><span class="special">,</span> <span class="identifier">addend2_type</span> <span class="keyword">const</span> <span class="special">&</span><span class="identifier">a2</span><span class="special">,</span> <span class="identifier">sum_type</span> <span class="special">&</span><span class="identifier">s</span> <span class="special">)</span> <span class="keyword">const</span><span class="special">;</span> 99<span class="special">};</span> 100 101<span class="comment">//...</span> 102 103<span class="keyword">int</span> <span class="identifier">main</span><span class="special">()</span> 104<span class="special">{</span> 105 <span class="keyword">int</span> <span class="keyword">const</span> <span class="identifier">a1</span><span class="special">[]</span> <span class="special">=</span> <span class="special">{</span> <span class="number">0</span><span class="special">,</span> <span class="number">4</span><span class="special">,</span> <span class="number">3</span> <span class="special">};</span> <span class="comment">// 340</span> 106 <span class="keyword">int</span> <span class="keyword">const</span> <span class="identifier">a2</span><span class="special">[]</span> <span class="special">=</span> <span class="special">{</span> <span class="number">9</span><span class="special">,</span> <span class="number">8</span> <span class="special">};</span> <span class="comment">// 89</span> 107 <span class="keyword">int</span> <span class="identifier">s</span><span class="special">[</span> <span class="number">4</span> <span class="special">];</span> 108 <span class="identifier">adder</span><span class="special"><</span><span class="number">3</span><span class="special">,</span><span class="number">2</span><span class="special">></span> <span class="identifier">obj</span><span class="special">;</span> 109 110 <span class="identifier">obj</span><span class="special">(</span> <span class="identifier">a1</span><span class="special">,</span> <span class="identifier">a2</span><span class="special">,</span> <span class="identifier">s</span> <span class="special">);</span> <span class="comment">// 's' should be 429 or { 9, 2, 4, 0 }</span> 111 <span class="comment">//...</span> 112<span class="special">}</span> 113</pre> 114</div> 115<div class="section"> 116<div class="titlepage"><div><div><h3 class="title"> 117<a name="boost_integer.minmax.demonstration_program"></a><a class="link" href="minmax.html#boost_integer.minmax.demonstration_program" title="Demonstration Program">Demonstration 118 Program</a> 119</h3></div></div></div> 120<p> 121 The program <a href="../../../test/static_min_max_test.cpp" target="_top">static_min_max_test.cpp</a> 122 is a simplistic demonstration of various comparisons using the compile-time 123 extrema class templates. 124 </p> 125</div> 126<div class="section"> 127<div class="titlepage"><div><div><h3 class="title"> 128<a name="boost_integer.minmax.rationale"></a><a class="link" href="minmax.html#boost_integer.minmax.rationale" title="Rationale">Rationale</a> 129</h3></div></div></div> 130<p> 131 Sometimes the minimum or maximum of several values needs to be found for 132 later compile-time processing, <span class="emphasis"><em>e.g.</em></span> for a bound for 133 another class template. 134 </p> 135</div> 136<div class="section"> 137<div class="titlepage"><div><div><h3 class="title"> 138<a name="boost_integer.minmax.credits"></a><a class="link" href="minmax.html#boost_integer.minmax.credits" title="Credits">Credits</a> 139</h3></div></div></div> 140<p> 141 The author of the Boost compile-time extrema class templates is <a href="http://www.boost.org/people/daryle_walker.html" target="_top">Daryle 142 Walker</a>. 143 </p> 144</div> 145</div> 146<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 147<td align="left"></td> 148<td align="right"><div class="copyright-footer">Copyright © 2001-2009 Beman 149 Dawes, Daryle Walker, Gennaro Prota, John Maddock<p> 150 Distributed under the Boost Software License, Version 1.0. (See accompanying 151 file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>) 152 </p> 153</div></td> 154</tr></table> 155<hr> 156<div class="spirit-nav"> 157<a accesskey="p" href="log2.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.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="history.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a> 158</div> 159</body> 160</html> 161