mirror of
https://github.com/boostorg/integer.git
synced 2025-07-17 14:42:07 +02:00
122 lines
3.7 KiB
HTML
122 lines
3.7 KiB
HTML
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
|
|
<html>
|
|
<head>
|
|
<title>Compile-Time Extrema Templates</title>
|
|
</head>
|
|
|
|
<body bgcolor="white" text="black" link="blue" alink="red" vlink="purple">
|
|
<h1><img src="../../../c++boost.gif" alt="c++boost.gif (8819 bytes)"
|
|
align="middle" width="277" height="86">Compile-Time Extrema
|
|
Templates</h1>
|
|
|
|
<p>The class templates in <cite><a
|
|
href="../../../boost/integer/static_min_max.hpp"><boost/integer/static_min_max.hpp></a></cite>
|
|
provide a compile-time evaluation of the minimum or maximum of
|
|
two integers. These facilities are useful for generic programming problems.</p>
|
|
|
|
<h2><a name="contents">Contents</a></h2>
|
|
|
|
<ul>
|
|
<li><a href="#contents">Contents</a></li>
|
|
<li><a href="#synopsis">Synopsis</a></li>
|
|
<li><a href="#usage">Usage</a></li>
|
|
<li><a href="#example">Example</a></li>
|
|
<li><a href="#demo">Demonstration Program</a></li>
|
|
<li><a href="#rationale">Rationale</a></li>
|
|
<li><a href="#credits">Credits</a></li>
|
|
</ul>
|
|
|
|
<h2><a name="synopsis">Synopsis</a></h2>
|
|
|
|
<blockquote><pre>
|
|
namespace boost
|
|
{
|
|
|
|
template < long Value1, long Value2 >
|
|
struct static_signed_min;
|
|
|
|
template < long Value1, long Value2 >
|
|
struct static_signed_max;
|
|
|
|
template < unsigned long Value1, unsigned long Value2 >
|
|
struct static_unsigned_min;
|
|
|
|
template < unsigned long Value1, unsigned long Value2 >
|
|
struct static_unsigned_max;
|
|
|
|
}
|
|
</pre></blockquote>
|
|
|
|
<h2><a name="usage">Usage</a></h2>
|
|
|
|
<p>The four class templates provide the combinations for finding the
|
|
minimum or maximum of two signed or <code>unsigned</code>
|
|
(<code>long</code>) parameters, <var>Value1</var> and <var>Value2</var>,
|
|
at compile-time. Each template has a single static data member,
|
|
<code>value</code>, which is set to the respective minimum or maximum
|
|
of the template's parameters.</p>
|
|
|
|
<h2><a name="example">Example</a></h2>
|
|
|
|
<blockquote><pre>
|
|
#include <boost/integer/static_min_max.hpp>
|
|
|
|
template < unsigned long AddendSize1, unsigned long AddendSize2 >
|
|
class adder
|
|
{
|
|
public:
|
|
static unsigned long const addend1_size = AddendSize1;
|
|
static unsigned long const addend2_size = AddendSize2;
|
|
static unsigned long const sum_size = boost::static_unsigned_max<AddendSize1, AddendSize2>::value + 1;
|
|
|
|
typedef int addend1_type[ addend1_size ];
|
|
typedef int addend2_type[ addend2_size ];
|
|
typedef int sum_type[ sum_size ];
|
|
|
|
void operator ()( addend1_type const &a1, addend2_type const &a2, sum_type &s ) const;
|
|
};
|
|
|
|
//...
|
|
|
|
int main()
|
|
{
|
|
int const a1[] = { 0, 4, 3 }; // 340
|
|
int const a2[] = { 9, 8 }; // 89
|
|
int s[ 4 ];
|
|
adder<3,2> obj;
|
|
|
|
obj( a1, a2, s ); // 's' should be 429 or { 9, 2, 4, 0 }
|
|
//...
|
|
}
|
|
</pre></blockquote>
|
|
|
|
<h2><a name="demo">Demonstration Program</a></h2>
|
|
|
|
<p>The program <a
|
|
href="../test/static_min_max_test.cpp">static_min_max_test.cpp</a> is a
|
|
simplistic demonstration of various comparisons using the compile-time
|
|
extrema class templates.</p>
|
|
|
|
<h2><a name="rationale">Rationale</a></h2>
|
|
|
|
<p>Sometimes the minimum or maximum of several values needs to be found
|
|
for later compile-time processing, <i>e.g.</i> for a bound for another
|
|
class template.</p>
|
|
|
|
<h2><a name="credits">Credits</a></h2>
|
|
|
|
<p>The author of the Boost compile-time extrema class templates is <a
|
|
href="../../../people/daryle_walker.html">Daryle Walker</a>.</p>
|
|
|
|
<hr>
|
|
|
|
<p>Revised October 12, 2001</p>
|
|
|
|
<p>© Copyright Daryle Walker 2001. Permission to copy, use,
|
|
modify, sell and distribute this document is granted provided this
|
|
copyright notice appears in all copies. This document is provided
|
|
"as is" without express or implied warranty, and with no claim
|
|
as to its suitability for any purpose.</p>
|
|
</body>
|
|
</html>
|