2001-02-12 11:17:54 +08:00
|
|
|
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2//EN">
|
|
|
|
|
|
|
|
|
|
<meta name="generator" content="HTML Tidy, see www.w3.org">
|
|
|
|
|
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
|
|
|
|
|
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
|
|
|
|
|
<meta name="ProgId" content="FrontPage.Editor.Document">
|
|
|
|
|
|
|
|
|
|
<title>Generic Programming Techniques</title>
|
|
|
|
|
|
|
|
|
|
<img src="../c++boost.gif" alt="c++boost.gif (8819 bytes)" align="center"
|
|
|
|
|
width="277" height="86">
|
|
|
|
|
|
2001-03-09 05:41:55 +08:00
|
|
|
|
<body bgcolor="#FFFFFF" text="#000000">
|
|
|
|
|
|
2001-02-12 11:17:54 +08:00
|
|
|
|
<h1>Generic Programming Techniques</h1>
|
|
|
|
|
|
|
|
|
|
<p>This is an incomplete survey of some of the generic programming
|
|
|
|
|
techniques used in the <a href="../index.htm">boost</a> libraries.
|
|
|
|
|
|
|
|
|
|
<h2>Table of Contents</h2>
|
|
|
|
|
|
|
|
|
|
<ul>
|
2001-02-14 01:29:39 +08:00
|
|
|
|
<li><a href="#introduction">Introduction</a>
|
|
|
|
|
|
|
|
|
|
<li><a href="#concept">The Anatomy of a Concept</a>
|
|
|
|
|
|
2001-02-12 11:17:54 +08:00
|
|
|
|
<li><a href="#traits">Traits</a>
|
|
|
|
|
|
2001-02-12 13:04:28 +08:00
|
|
|
|
<li><a href="#tag_dispatching">Tag Dispatching</a>
|
|
|
|
|
|
2001-02-14 01:50:49 +08:00
|
|
|
|
<li><a href="#adaptors">Adaptors</a>
|
|
|
|
|
|
2001-02-12 11:17:54 +08:00
|
|
|
|
<li><a href="#type_generator">Type Generators</a>
|
|
|
|
|
|
2001-02-12 12:03:20 +08:00
|
|
|
|
<li><a href="#object_generator">Object Generators</a>
|
2001-02-12 11:17:54 +08:00
|
|
|
|
|
2001-02-15 22:32:18 +08:00
|
|
|
|
<li><a href="#policy">Policy Classes</a>
|
2001-02-12 11:17:54 +08:00
|
|
|
|
</ul>
|
|
|
|
|
|
2001-02-15 22:32:18 +08:00
|
|
|
|
<h2><a name="introduction">Introduction</a></h2>
|
2001-02-14 01:29:39 +08:00
|
|
|
|
|
2001-02-15 22:32:18 +08:00
|
|
|
|
<p>Generic programming is about generalizing software components so that
|
|
|
|
|
they can be easily reused in a wide variety of situations. In C++, class
|
|
|
|
|
and function templates are particularly effective mechanisms for generic
|
|
|
|
|
programming because they make the generalization possible without
|
|
|
|
|
sacrificing efficiency.
|
2001-02-14 01:29:39 +08:00
|
|
|
|
|
2001-02-15 22:32:18 +08:00
|
|
|
|
<p>As a simple example of generic programming, we will look at how one
|
|
|
|
|
might generalize the <tt>memcpy()</tt> function of the C standard library.
|
|
|
|
|
An implementation of <tt>memcpy()</tt> might look like the following:
|
|
|
|
|
<br>
|
|
|
|
|
<br>
|
2001-02-14 01:29:39 +08:00
|
|
|
|
|
2001-02-15 22:32:18 +08:00
|
|
|
|
<blockquote>
|
2001-02-14 01:29:39 +08:00
|
|
|
|
<pre>
|
|
|
|
|
void* memcpy(void* region1, const void* region2, size_t n)
|
|
|
|
|
{
|
|
|
|
|
const char* first = (const char*)region2;
|
|
|
|
|
const char* last = ((const char*)region2) + n;
|
|
|
|
|
char* result = (char*)region1;
|
|
|
|
|
while (first != last)
|
|
|
|
|
*result++ = *first++;
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
</pre>
|
2001-02-15 22:32:18 +08:00
|
|
|
|
</blockquote>
|
|
|
|
|
The <tt>memcpy()</tt> function is already generalized to some extent by the
|
|
|
|
|
use of <tt>void*</tt> so that the function can be used to copy arrays of
|
|
|
|
|
different kinds of data. But what if the data we would like to copy is not
|
|
|
|
|
in an array? Perhaps it is in a linked list. Can we generalize the notion
|
|
|
|
|
of copy to any sequence of elements? Looking at the body of
|
|
|
|
|
<tt>memcpy()</tt>, the function's <b><i>minimal requirements</i></b> are
|
|
|
|
|
that it needs to to <i>traverse</i> through the sequence using some sort of
|
|
|
|
|
pointer, <i>access</i> elements pointed to, <i>write</i> the elements to
|
|
|
|
|
the destination, and <i>compare</i> pointers to know when to stop. The C++
|
|
|
|
|
standard library groups requirements such as these into
|
|
|
|
|
<b><i>concepts</i></b>, in this case the <a href=
|
|
|
|
|
"http://www.sgi.com/tech/stl/InputIterator.html">Input Iterator</a> concept
|
|
|
|
|
(for <tt>region2</tt>) and the <a href=
|
|
|
|
|
"http://www.sgi.com/tech/stl/OutputIterator.html">Output Iterator</a>
|
|
|
|
|
concept (for <tt>region1</tt>).
|
|
|
|
|
|
|
|
|
|
<p>If we rewrite the <tt>memcpy()</tt> as a function template, and use the
|
|
|
|
|
<a href="http://www.sgi.com/tech/stl/InputIterator.html">Input Iterator</a>
|
|
|
|
|
and <a href="http://www.sgi.com/tech/stl/OutputIterator.html">Output
|
2001-02-14 01:29:39 +08:00
|
|
|
|
Iterator</a> concepts to describe the requirements on the template
|
2001-02-15 22:32:18 +08:00
|
|
|
|
parameters, we can implement a highly reusable <tt>copy()</tt> function in
|
|
|
|
|
the following way:
|
|
|
|
|
<br>
|
|
|
|
|
<br>
|
|
|
|
|
|
|
|
|
|
<blockquote>
|
2001-02-14 01:29:39 +08:00
|
|
|
|
<pre>
|
|
|
|
|
template <typename InputIterator, typename OutputIterator>
|
|
|
|
|
OutputIterator
|
|
|
|
|
copy(InputIterator first, InputIterator last, OutputIterator result)
|
|
|
|
|
{
|
|
|
|
|
while (first != last)
|
|
|
|
|
*result++ = *first++;
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
</pre>
|
2001-02-15 22:32:18 +08:00
|
|
|
|
</blockquote>
|
|
|
|
|
|
|
|
|
|
<p>Using the generic <tt>copy()</tt> function, we can now copy elements
|
|
|
|
|
from any kind of sequence, including a linked list that exports iterators
|
|
|
|
|
such as <tt>std::<a href=
|
|
|
|
|
"http://www.sgi.com/tech/stl/List.html">list</a></tt>.
|
|
|
|
|
<br>
|
|
|
|
|
<br>
|
|
|
|
|
|
|
|
|
|
<blockquote>
|
2001-02-14 01:29:39 +08:00
|
|
|
|
<pre>
|
|
|
|
|
#include <list>
|
|
|
|
|
#include <vector>
|
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
|
|
int main()
|
|
|
|
|
{
|
|
|
|
|
const int N = 3;
|
|
|
|
|
std::vector<int> region1(N);
|
|
|
|
|
std::list<int> region2;
|
|
|
|
|
|
|
|
|
|
region2.push_back(1);
|
|
|
|
|
region2.push_back(0);
|
|
|
|
|
region2.push_back(3);
|
|
|
|
|
|
|
|
|
|
std::copy(region2.begin(), region2.end(), region1.begin());
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < N; ++i)
|
|
|
|
|
std::cout << region1[i] << " ";
|
|
|
|
|
std::cout << std::endl;
|
|
|
|
|
}
|
|
|
|
|
</pre>
|
2001-02-15 22:32:18 +08:00
|
|
|
|
</blockquote>
|
2001-02-14 01:29:39 +08:00
|
|
|
|
|
|
|
|
|
<h2><a name="concept">Anatomy of a Concept</a></h2>
|
2001-02-15 22:32:18 +08:00
|
|
|
|
A <b><i>concept</i></b> is a set requirements, where the requirements
|
|
|
|
|
consist of valid expressions, associated types, invariants, and complexity
|
|
|
|
|
guarantees. A type that satisfies the set of requirements is said to
|
|
|
|
|
<b><i>model</i></b> the concept. A concept can extend the requirements of
|
|
|
|
|
another concept, which is called <b><i>refinement</i></b>.
|
2001-02-14 01:29:39 +08:00
|
|
|
|
|
|
|
|
|
<ul>
|
2001-02-15 22:32:18 +08:00
|
|
|
|
<li><a name="valid_expression"><b>Valid Expressions</b></a> are C++
|
|
|
|
|
expressions which must compile successfully for the objects involved in
|
|
|
|
|
the expression to be considered <i>models</i> of the concept.
|
|
|
|
|
|
|
|
|
|
<li><a name="associated_type"><b>Associated Types</b></a> are types that
|
|
|
|
|
are related to the modeling type in that they participate in one or more
|
|
|
|
|
of the valid expressions. Typically associated types can be accessed
|
|
|
|
|
either through typedefs nested within a class definition for the modeling
|
|
|
|
|
type, or they are accessed through a <a href="#traits">traits class</a>.
|
|
|
|
|
|
|
|
|
|
<li><b>Invariants</b> are run-time characteristics of the objects that
|
|
|
|
|
must always be true, that is, the functions involving the objects must
|
|
|
|
|
preserve these characteristics. The invariants often take the form of
|
|
|
|
|
pre-conditions and post-conditions.
|
|
|
|
|
|
|
|
|
|
<li><b>Complexity Guarantees</b> are maximum limits on how long the
|
|
|
|
|
execution of one of the valid expressions will take, or how much of
|
|
|
|
|
various resources its computation will use.
|
2001-02-14 01:29:39 +08:00
|
|
|
|
</ul>
|
|
|
|
|
|
2001-02-15 22:32:18 +08:00
|
|
|
|
<p>The concepts used in the C++ Standard Library are documented at the <a
|
|
|
|
|
href="http://www.sgi.com/tech/stl/table_of_contents.html">SGI STL site</a>.
|
2001-02-14 01:29:39 +08:00
|
|
|
|
|
2001-02-12 11:17:54 +08:00
|
|
|
|
<h2><a name="traits">Traits</a></h2>
|
|
|
|
|
|
2001-02-12 22:20:58 +08:00
|
|
|
|
<p>A traits class provides a way of associating information with a
|
|
|
|
|
compile-time entity (a type, integral constant, or address). For example,
|
|
|
|
|
the class template <tt><a href=
|
2001-02-12 11:17:54 +08:00
|
|
|
|
"http://www.sgi.com/tech/stl/iterator_traits.html">std::iterator_traits<T></a></tt>
|
|
|
|
|
looks something like this:
|
|
|
|
|
|
|
|
|
|
<blockquote>
|
|
|
|
|
<pre>
|
|
|
|
|
template <class Iterator>
|
|
|
|
|
struct iterator_traits {
|
|
|
|
|
typedef ... iterator_category;
|
|
|
|
|
typedef ... value_type;
|
|
|
|
|
typedef ... difference_type;
|
|
|
|
|
typedef ... pointer;
|
|
|
|
|
typedef ... reference;
|
|
|
|
|
};
|
|
|
|
|
</pre>
|
|
|
|
|
</blockquote>
|
|
|
|
|
The traits' <tt>value_type</tt> gives generic code the type which the
|
|
|
|
|
iterator is "pointing at", while the <tt>iterator_category</tt> can be used
|
|
|
|
|
to select more efficient algorithms depending on the iterator's
|
|
|
|
|
capabilities.
|
|
|
|
|
|
|
|
|
|
<p>A key feature of traits templates is that they're <i>non-intrusive</i>:
|
|
|
|
|
they allow us to associate information with arbitrary types, including
|
|
|
|
|
built-in types and types defined in third-party libraries, Normally, traits
|
|
|
|
|
are specified for a particular type by (partially) specializing the traits
|
|
|
|
|
template.
|
|
|
|
|
|
2001-02-15 22:32:18 +08:00
|
|
|
|
<p>For an in-depth description of <tt>std::iterator_traits</tt>, see <a
|
|
|
|
|
href="http://www.sgi.com/tech/stl/iterator_traits.html">this page</a>
|
|
|
|
|
provided by SGI. Another very different expression of the traits idiom in
|
|
|
|
|
the standard is <tt>std::numeric_limits<T></tt> which provides
|
|
|
|
|
constants describing the range and capabilities of numeric types.
|
2001-02-12 11:17:54 +08:00
|
|
|
|
|
2001-02-12 13:04:28 +08:00
|
|
|
|
<h2><a name="tag_dispatching">Tag Dispatching</a></h2>
|
|
|
|
|
|
2001-02-15 22:32:18 +08:00
|
|
|
|
<p>A technique that often goes hand in hand with traits classes is tag
|
|
|
|
|
dispatching, which is a way of using function overloading to dispatch based
|
2001-03-09 10:07:39 +08:00
|
|
|
|
on properties of a type. A good example of this is the implementation of the
|
|
|
|
|
<a href=
|
2001-02-15 22:32:18 +08:00
|
|
|
|
"http://www.sgi.com/tech/stl/advance.html"><tt>std::advance()</tt></a>
|
|
|
|
|
function in the C++ Standard Library, which increments an iterator
|
|
|
|
|
<tt>n</tt> times. Depending on the kind of iterator, there are different
|
|
|
|
|
optimizations that can be applied in the implementation. If the iterator is
|
|
|
|
|
<a href="http://www.sgi.com/tech/stl/RandomAccessIterator.html">random
|
|
|
|
|
access</a> (can jump forward and backward arbitrary distances), then the
|
|
|
|
|
<tt>advance()</tt> function can simply be implemented with <tt>i += n</tt>,
|
2001-03-09 10:07:39 +08:00
|
|
|
|
and is very efficient: constant time. Other iterators must be
|
|
|
|
|
<tt>advance</tt>d in steps, making the operation linear in n. If the
|
|
|
|
|
iterator is <a href=
|
2001-02-15 22:32:18 +08:00
|
|
|
|
"http://www.sgi.com/tech/stl/BidirectionalIterator.html">bidirectional</a>,
|
2001-03-09 10:07:39 +08:00
|
|
|
|
then it makes sense for <tt>n</tt> to be negative, so we must decide whether
|
|
|
|
|
to increment or decrement the iterator.
|
2001-02-15 22:32:18 +08:00
|
|
|
|
|
|
|
|
|
<p>The relation between tag dispatching and traits classes is that the
|
|
|
|
|
property used for dispatching (in this case the <tt>iterator_category</tt>)
|
2001-03-09 10:07:39 +08:00
|
|
|
|
is often accessed through a traits class. The main <tt>advance()</tt> function
|
2001-02-15 22:32:18 +08:00
|
|
|
|
uses the <a href=
|
|
|
|
|
"http://www.sgi.com/tech/stl/iterator_traits.html"><tt>iterator_traits</tt></a>
|
|
|
|
|
class to get the <tt>iterator_category</tt>. It then makes a call the the
|
|
|
|
|
overloaded <tt>advance_dispatch()</tt> function. The appropriate
|
|
|
|
|
<tt>advance_dispatch()</tt> is selected by the compiler based on whatever
|
|
|
|
|
type the <tt>iterator_category</tt> resolves to, either <a href=
|
|
|
|
|
"http://www.sgi.com/tech/stl/input_iterator_tag.html"><tt>input_iterator_tag</tt></a>,
|
|
|
|
|
<a href=
|
|
|
|
|
"http://www.sgi.com/tech/stl/bidirectional_iterator_tag.html"><tt>bidirectional_iterator_tag</tt></a>,
|
|
|
|
|
or <a href=
|
|
|
|
|
"http://www.sgi.com/tech/stl/random_access_iterator_tag.html"><tt>random_access_iterator_tag</tt></a>.
|
|
|
|
|
A <b><i>tag</i></b> is simply a class whose only purpose is to convey some
|
|
|
|
|
property for use in tag dispatching and similar techniques. Refer to <a
|
|
|
|
|
href="http://www.sgi.com/tech/stl/iterator_tags.html">this page</a> for a
|
|
|
|
|
more detailed description of iterator tags.
|
|
|
|
|
|
2001-02-12 13:04:28 +08:00
|
|
|
|
<blockquote>
|
|
|
|
|
<pre>
|
|
|
|
|
namespace std {
|
|
|
|
|
struct input_iterator_tag { };
|
|
|
|
|
struct bidirectional_iterator_tag { };
|
|
|
|
|
struct random_access_iterator_tag { };
|
|
|
|
|
|
|
|
|
|
namespace detail {
|
|
|
|
|
template <class InputIterator, class Distance>
|
|
|
|
|
void advance_dispatch(InputIterator& i, Distance n, <b>input_iterator_tag</b>) {
|
|
|
|
|
while (n--) ++i;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <class BidirectionalIterator, class Distance>
|
|
|
|
|
void advance_dispatch(BidirectionalIterator& i, Distance n,
|
2001-02-15 22:32:18 +08:00
|
|
|
|
<b>bidirectional_iterator_tag</b>) {
|
2001-02-12 13:04:28 +08:00
|
|
|
|
if (n >= 0)
|
2001-03-09 10:07:39 +08:00
|
|
|
|
while (n--) ++i;
|
2001-02-12 13:04:28 +08:00
|
|
|
|
else
|
2001-03-09 10:07:39 +08:00
|
|
|
|
while (n++) --i;
|
2001-02-12 13:04:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <class RandomAccessIterator, class Distance>
|
|
|
|
|
void advance_dispatch(RandomAccessIterator& i, Distance n,
|
2001-02-15 22:32:18 +08:00
|
|
|
|
<b>random_access_iterator_tag</b>) {
|
2001-02-12 13:04:28 +08:00
|
|
|
|
i += n;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <class InputIterator, class Distance>
|
|
|
|
|
void advance(InputIterator& i, Distance n) {
|
|
|
|
|
typename <b>iterator_traits<InputIterator>::iterator_category</b> category;
|
|
|
|
|
detail::advance_dispatch(i, n, <b>category</b>);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
</pre>
|
|
|
|
|
</blockquote>
|
|
|
|
|
|
2001-02-14 01:50:49 +08:00
|
|
|
|
<h2><a name="adaptors">Adaptors</a></h2>
|
|
|
|
|
|
|
|
|
|
<p>An <i>adaptor</i> is a class template which builds on another type or
|
|
|
|
|
types to provide a new interface or behavioral variant. Examples of
|
|
|
|
|
standard adaptors are <a href=
|
|
|
|
|
"http://www.sgi.com/tech/stl/ReverseIterator.html">std::reverse_iterator</a>,
|
|
|
|
|
which adapts an iterator type by reversing its motion upon
|
|
|
|
|
increment/decrement, and <a href=
|
|
|
|
|
"http://www.sgi.com/tech/stl/stack.html">std::stack</a>, which adapts a
|
|
|
|
|
container to provide a simple stack interface.
|
|
|
|
|
|
|
|
|
|
<p>A more comprehensive review of the adaptors in the standard can be found
|
|
|
|
|
<a href=
|
|
|
|
|
"http://www.cs.rpi.edu/~wiseb/xrds/ovp2-3b.html#SECTION00015000000000000000">
|
|
|
|
|
here</a>.
|
|
|
|
|
|
2001-02-12 11:17:54 +08:00
|
|
|
|
<h2><a name="type_generator">Type Generators</a></h2>
|
|
|
|
|
|
|
|
|
|
<p>A <i>type generator</i> is a template whose only purpose is to
|
2001-02-17 12:28:59 +08:00
|
|
|
|
synthesize a new type or types based on its template argument(s)<a href=
|
2001-02-15 22:32:18 +08:00
|
|
|
|
"#1">[1]</a>. The generated type is usually expressed as a nested typedef
|
|
|
|
|
named, appropriately <tt>type</tt>. A type generator is usually used to
|
|
|
|
|
consolidate a complicated type expression into a simple one, as in
|
|
|
|
|
<tt>boost::<a href=
|
2001-03-14 23:11:55 +08:00
|
|
|
|
"../libs/utility/filter_iterator.htm">filter_iterator_generator</a></tt>,
|
2001-02-12 11:17:54 +08:00
|
|
|
|
which looks something like this:
|
|
|
|
|
|
|
|
|
|
<blockquote>
|
|
|
|
|
<pre>
|
|
|
|
|
template <class Predicate, class Iterator,
|
|
|
|
|
class Value = <i>complicated default</i>,
|
|
|
|
|
class Reference = <i>complicated default</i>,
|
|
|
|
|
class Pointer = <i>complicated default</i>,
|
|
|
|
|
class Category = <i>complicated default</i>,
|
|
|
|
|
class Distance = <i>complicated default</i>
|
|
|
|
|
>
|
|
|
|
|
struct filter_iterator_generator {
|
|
|
|
|
typedef iterator_adaptor<
|
|
|
|
|
Iterator,filter_iterator_policies<Predicate,Iterator>,
|
|
|
|
|
Value,Reference,Pointer,Category,Distance> <b>type</b>;
|
|
|
|
|
};
|
|
|
|
|
</pre>
|
|
|
|
|
</blockquote>
|
|
|
|
|
|
|
|
|
|
<p>Now, that's complicated, but producing an adapted filter iterator is
|
|
|
|
|
much easier. You can usually just write:
|
|
|
|
|
|
|
|
|
|
<blockquote>
|
|
|
|
|
<pre>
|
|
|
|
|
boost::filter_iterator_generator<my_predicate,my_base_iterator>::type
|
|
|
|
|
</pre>
|
|
|
|
|
</blockquote>
|
|
|
|
|
|
|
|
|
|
<h2><a name="object_generator">Object Generators</a></h2>
|
|
|
|
|
|
|
|
|
|
<p>An <i>object generator</i> is a function template whose only purpose is
|
|
|
|
|
to construct a new object out of its arguments. Think of it as a kind of
|
|
|
|
|
generic constructor. An object generator may be more useful than a plain
|
|
|
|
|
constructor when the exact type to be generated is difficult or impossible
|
|
|
|
|
to express and the result of the generator can be passed directly to a
|
2001-02-15 22:32:18 +08:00
|
|
|
|
function rather than stored in a variable. Most Boost object generators are
|
|
|
|
|
named with the prefix "<tt>make_</tt>", after <tt>std::<a href=
|
2001-02-12 11:17:54 +08:00
|
|
|
|
"http://www.sgi.com/tech/stl/pair.html">make_pair</a>(const<73>T&,<2C>const<73>U&)</tt>.
|
|
|
|
|
|
2001-02-15 22:32:18 +08:00
|
|
|
|
<p>For example, given:
|
2001-02-12 11:17:54 +08:00
|
|
|
|
|
|
|
|
|
<blockquote>
|
|
|
|
|
<pre>
|
2001-02-15 22:32:18 +08:00
|
|
|
|
struct widget {
|
|
|
|
|
void tweak(int);
|
|
|
|
|
};
|
|
|
|
|
std::vector<widget *> widget_ptrs;
|
|
|
|
|
</pre>
|
|
|
|
|
</blockquote>
|
|
|
|
|
By chaining two standard object generators, <tt>std::<a href=
|
|
|
|
|
"http://www.dinkumware.com/htm_cpl/functio2.html#bind2nd">bind2nd</a>()</tt>
|
|
|
|
|
and <tt>std::<a href=
|
|
|
|
|
"http://www.dinkumware.com/htm_cpl/functio2.html#mem_fun">mem_fun</a>()</tt>,
|
|
|
|
|
we can easily tweak all widgets:
|
|
|
|
|
|
|
|
|
|
<blockquote>
|
|
|
|
|
<pre>
|
|
|
|
|
void tweak_all_widgets1(int arg)
|
2001-02-12 11:17:54 +08:00
|
|
|
|
{
|
2001-02-15 22:32:18 +08:00
|
|
|
|
for_each(widget_ptrs.begin(), widget_ptrs.end(),
|
|
|
|
|
<b>bind2nd</b>(std::<b>mem_fun</b>(&widget::tweak), arg));
|
2001-02-12 11:17:54 +08:00
|
|
|
|
}
|
|
|
|
|
</pre>
|
|
|
|
|
</blockquote>
|
|
|
|
|
|
2001-02-15 22:32:18 +08:00
|
|
|
|
<p>Without using object generators the example above would look like this:
|
2001-02-12 11:17:54 +08:00
|
|
|
|
|
|
|
|
|
<blockquote>
|
|
|
|
|
<pre>
|
2001-02-15 22:32:18 +08:00
|
|
|
|
void tweak_all_widgets2(int arg)
|
2001-02-12 11:17:54 +08:00
|
|
|
|
{
|
2001-02-15 22:32:18 +08:00
|
|
|
|
for_each(struct_ptrs.begin(), struct_ptrs.end(),
|
|
|
|
|
<b>std::binder2nd<std::mem_fun1_t<void, widget, int> ></b>(
|
|
|
|
|
std::<b>mem_fun1_t<void, widget, int></b>(&widget::tweak), arg));
|
2001-02-12 11:17:54 +08:00
|
|
|
|
}
|
|
|
|
|
</pre>
|
|
|
|
|
</blockquote>
|
|
|
|
|
|
|
|
|
|
<p>As expressions get more complicated the need to reduce the verbosity of
|
|
|
|
|
type specification gets more compelling.
|
|
|
|
|
|
2001-02-15 22:32:18 +08:00
|
|
|
|
<h2><a name="policy">Policy Classes</a></h2>
|
2001-02-12 11:17:54 +08:00
|
|
|
|
|
2001-02-15 22:32:18 +08:00
|
|
|
|
<p>A policy class is a template parameter used to transmit behavior. An
|
|
|
|
|
example from the standard library is <tt>std::<a href=
|
|
|
|
|
"http://www.dinkumware.com/htm_cpl/memory.html#allocator">allocator</a></tt>,
|
|
|
|
|
which supplies memory management behaviors to standard <a href=
|
|
|
|
|
"http://www.sgi.com/tech/stl/Container.html">containers</a>.
|
2001-02-12 22:20:58 +08:00
|
|
|
|
|
2001-02-15 22:32:18 +08:00
|
|
|
|
<p>Policy classes have been explored in detail by <a href=
|
2001-02-12 22:20:58 +08:00
|
|
|
|
"mailto:andrewalex@hotmail.com">Andrei Alexandrescu</a> in <a href=
|
2001-02-12 11:17:54 +08:00
|
|
|
|
"http://www.cs.ualberta.ca/~hoover/cmput401/XP-Notes/xp-conf/Papers/7_3_Alexandrescu.pdf">
|
|
|
|
|
this paper</a>. He writes:
|
|
|
|
|
|
|
|
|
|
<blockquote>
|
|
|
|
|
<p>Policy classes are implementations of punctual design choices. They
|
|
|
|
|
are inherited from, or contained within, other classes. They provide
|
|
|
|
|
different strategies under the same syntactic interface. A class using
|
|
|
|
|
policies is templated having one template parameter for each policy it
|
|
|
|
|
uses. This allows the user to select the policies needed.
|
|
|
|
|
|
|
|
|
|
<p>The power of policy classes comes from their ability to combine
|
|
|
|
|
freely. By combining several policy classes in a template class with
|
|
|
|
|
multiple parameters, one achieves combinatorial behaviors with a linear
|
|
|
|
|
amount of code.
|
|
|
|
|
</blockquote>
|
|
|
|
|
|
2001-02-15 22:32:18 +08:00
|
|
|
|
<p>Andrei's description of policy classes describe their power as being
|
|
|
|
|
derived from their granularity and orthogonality. Boost has probably
|
|
|
|
|
diluted the distinction in the <a href=
|
|
|
|
|
"../libs/utility/iterator_adaptors.htm">Iterator Adaptors</a> library,
|
|
|
|
|
where we transmit all of an adapted iterator's behavior in a single policy
|
|
|
|
|
class. There is precedent for this, however: <tt><a href=
|
|
|
|
|
"http://www.dinkumware.com/htm_cpl/string2.html#char_traits">std::char_traits</a></tt>,
|
2001-02-12 22:20:58 +08:00
|
|
|
|
despite its name, acts as a policies class that determines the behaviors of
|
2001-02-15 22:32:18 +08:00
|
|
|
|
<a href=
|
|
|
|
|
"http://www.dinkumware.com/htm_cpl/string2.html#basic_string">std::basic_string</a>.
|
2001-02-12 11:17:54 +08:00
|
|
|
|
|
2001-02-13 12:32:00 +08:00
|
|
|
|
<h2>Notes</h2>
|
2001-02-15 22:32:18 +08:00
|
|
|
|
<a name="1">[1]</a> Type generators are a workaround for the lack of
|
|
|
|
|
``templated typedefs'' in C++.
|
2001-02-12 12:03:20 +08:00
|
|
|
|
<hr>
|
|
|
|
|
|
|
|
|
|
<p>Revised
|
2001-03-14 23:11:55 +08:00
|
|
|
|
<!--webbot bot="Timestamp" s-type="EDITED" s-format="%d %b %Y" startspan -->14 Mar 2001<!--webbot bot="Timestamp" endspan i-checksum="14885" -->
|
2001-02-12 12:03:20 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<p>© Copyright David Abrahams 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
|
2001-02-15 22:32:18 +08:00
|
|
|
|
implied warranty, and with no claim as to its suitability for any purpose.
|
|
|
|
|
<!-- LocalWords: HTML html charset gif alt htm struct SGI namespace std libs
|
|
|
|
|
-->
|
|
|
|
|
<!-- LocalWords: InputIterator BidirectionalIterator RandomAccessIterator pdf
|
|
|
|
|
-->
|
|
|
|
|
<!-- LocalWords: typename Alexandrescu templated Andrei's Abrahams memcpy int
|
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
<!-- LocalWords: const OutputIterator iostream pre cpl
|
|
|
|
|
-->
|
|
|
|
|
|
2001-03-09 05:41:55 +08:00
|
|
|
|
</body>
|