[29] | 1 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN"> |
---|
| 2 | <html> |
---|
| 3 | <head> |
---|
| 4 | <title>Compile-Time Extrema Templates</title> |
---|
| 5 | </head> |
---|
| 6 | |
---|
| 7 | <body bgcolor="white" text="black" link="blue" alink="red" vlink="purple"> |
---|
| 8 | <h1><img src="../../../boost.png" alt="boost.png (6897 bytes)" |
---|
| 9 | align="middle" width="277" height="86">Compile-Time Extrema |
---|
| 10 | Templates</h1> |
---|
| 11 | |
---|
| 12 | <p>The class templates in <cite><a |
---|
| 13 | href="../../../boost/integer/static_min_max.hpp"><boost/integer/static_min_max.hpp></a></cite> |
---|
| 14 | provide a compile-time evaluation of the minimum or maximum of |
---|
| 15 | two integers. These facilities are useful for generic programming problems.</p> |
---|
| 16 | |
---|
| 17 | <h2><a name="contents">Contents</a></h2> |
---|
| 18 | |
---|
| 19 | <ul> |
---|
| 20 | <li><a href="#contents">Contents</a></li> |
---|
| 21 | <li><a href="#synopsis">Synopsis</a></li> |
---|
| 22 | <li><a href="#usage">Usage</a></li> |
---|
| 23 | <li><a href="#example">Example</a></li> |
---|
| 24 | <li><a href="#demo">Demonstration Program</a></li> |
---|
| 25 | <li><a href="#rationale">Rationale</a></li> |
---|
| 26 | <li><a href="#credits">Credits</a></li> |
---|
| 27 | </ul> |
---|
| 28 | |
---|
| 29 | <h2><a name="synopsis">Synopsis</a></h2> |
---|
| 30 | |
---|
| 31 | <blockquote><pre> |
---|
| 32 | namespace boost |
---|
| 33 | { |
---|
| 34 | |
---|
| 35 | template < long Value1, long Value2 > |
---|
| 36 | struct static_signed_min; |
---|
| 37 | |
---|
| 38 | template < long Value1, long Value2 > |
---|
| 39 | struct static_signed_max; |
---|
| 40 | |
---|
| 41 | template < unsigned long Value1, unsigned long Value2 > |
---|
| 42 | struct static_unsigned_min; |
---|
| 43 | |
---|
| 44 | template < unsigned long Value1, unsigned long Value2 > |
---|
| 45 | struct static_unsigned_max; |
---|
| 46 | |
---|
| 47 | } |
---|
| 48 | </pre></blockquote> |
---|
| 49 | |
---|
| 50 | <h2><a name="usage">Usage</a></h2> |
---|
| 51 | |
---|
| 52 | <p>The four class templates provide the combinations for finding the |
---|
| 53 | minimum or maximum of two signed or <code>unsigned</code> |
---|
| 54 | (<code>long</code>) parameters, <var>Value1</var> and <var>Value2</var>, |
---|
| 55 | at compile-time. Each template has a single static data member, |
---|
| 56 | <code>value</code>, which is set to the respective minimum or maximum |
---|
| 57 | of the template's parameters.</p> |
---|
| 58 | |
---|
| 59 | <h2><a name="example">Example</a></h2> |
---|
| 60 | |
---|
| 61 | <blockquote><pre> |
---|
| 62 | #include <boost/integer/static_min_max.hpp> |
---|
| 63 | |
---|
| 64 | template < unsigned long AddendSize1, unsigned long AddendSize2 > |
---|
| 65 | class adder |
---|
| 66 | { |
---|
| 67 | public: |
---|
| 68 | static unsigned long const addend1_size = AddendSize1; |
---|
| 69 | static unsigned long const addend2_size = AddendSize2; |
---|
| 70 | static unsigned long const sum_size = boost::static_unsigned_max<AddendSize1, AddendSize2>::value + 1; |
---|
| 71 | |
---|
| 72 | typedef int addend1_type[ addend1_size ]; |
---|
| 73 | typedef int addend2_type[ addend2_size ]; |
---|
| 74 | typedef int sum_type[ sum_size ]; |
---|
| 75 | |
---|
| 76 | void operator ()( addend1_type const &a1, addend2_type const &a2, sum_type &s ) const; |
---|
| 77 | }; |
---|
| 78 | |
---|
| 79 | //... |
---|
| 80 | |
---|
| 81 | int main() |
---|
| 82 | { |
---|
| 83 | int const a1[] = { 0, 4, 3 }; // 340 |
---|
| 84 | int const a2[] = { 9, 8 }; // 89 |
---|
| 85 | int s[ 4 ]; |
---|
| 86 | adder<3,2> obj; |
---|
| 87 | |
---|
| 88 | obj( a1, a2, s ); // 's' should be 429 or { 9, 2, 4, 0 } |
---|
| 89 | //... |
---|
| 90 | } |
---|
| 91 | </pre></blockquote> |
---|
| 92 | |
---|
| 93 | <h2><a name="demo">Demonstration Program</a></h2> |
---|
| 94 | |
---|
| 95 | <p>The program <a |
---|
| 96 | href="../test/static_min_max_test.cpp">static_min_max_test.cpp</a> is a |
---|
| 97 | simplistic demonstration of various comparisons using the compile-time |
---|
| 98 | extrema class templates.</p> |
---|
| 99 | |
---|
| 100 | <h2><a name="rationale">Rationale</a></h2> |
---|
| 101 | |
---|
| 102 | <p>Sometimes the minimum or maximum of several values needs to be found |
---|
| 103 | for later compile-time processing, <i>e.g.</i> for a bound for another |
---|
| 104 | class template.</p> |
---|
| 105 | |
---|
| 106 | <h2><a name="credits">Credits</a></h2> |
---|
| 107 | |
---|
| 108 | <p>The author of the Boost compile-time extrema class templates is <a |
---|
| 109 | href="../../../people/daryle_walker.html">Daryle Walker</a>.</p> |
---|
| 110 | |
---|
| 111 | <hr> |
---|
| 112 | |
---|
| 113 | <p>Revised October 12, 2001</p> |
---|
| 114 | |
---|
| 115 | <p>© Copyright Daryle Walker 2001. Permission to copy, use, |
---|
| 116 | modify, sell and distribute this document is granted provided this |
---|
| 117 | copyright notice appears in all copies. This document is provided |
---|
| 118 | "as is" without express or implied warranty, and with no claim |
---|
| 119 | as to its suitability for any purpose.</p> |
---|
| 120 | </body> |
---|
| 121 | </html> |
---|