<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="Docutils 0.3.9: http://docutils.sourceforge.net/" />
<title>14 crazy ideas for the standard library in C++0x</title>
<meta name="author" content="Thorsten Ottosen" />
<meta name="date" content="2005-08-24" />
<style type="text/css">

/*
:Author: David Goodger
:Contact: goodger@users.sourceforge.net
:date: $Date: 2004/01/12 02:49:54 $
:version: $Revision: 1.2 $
:copyright: This stylesheet has been placed in the public domain.

Default cascading style sheet for the HTML output of Docutils.
*/

.first {
  margin-top: 0 }

.last {
  margin-bottom: 0 }

a.toc-backref {
  text-decoration: none ;
  color: black }

dd {
  margin-bottom: 0.5em }

div.abstract {
  margin: 2em 5em }

div.abstract p.topic-title {
  font-weight: bold ;
  text-align: center }

div.attention, div.caution, div.danger, div.error, div.hint,
div.important, div.note, div.tip, div.warning, div.admonition {
  margin: 2em ;
  border: medium outset ;
  padding: 1em }

div.attention p.admonition-title, div.caution p.admonition-title,
div.danger p.admonition-title, div.error p.admonition-title,
div.warning p.admonition-title {
  color: red ;
  font-weight: bold ;
  font-family: sans-serif }

div.hint p.admonition-title, div.important p.admonition-title,
div.note p.admonition-title, div.tip p.admonition-title,
div.admonition p.admonition-title {
  font-weight: bold ;
  font-family: sans-serif }

div.dedication {
  margin: 2em 5em ;
  text-align: center ;
  font-style: italic }

div.dedication p.topic-title {
  font-weight: bold ;
  font-style: normal }

div.figure {
  margin-left: 2em }

div.footer, div.header {
  font-size: smaller }

div.sidebar {
  margin-left: 1em ;
  border: medium outset ;
  padding: 0em 1em ;
  background-color: #ffffee ;
  width: 40% ;
  float: right ;
  clear: right }

div.sidebar p.rubric {
  font-family: sans-serif ;
  font-size: medium }

div.system-messages {
  margin: 5em }

div.system-messages h1 {
  color: red }

div.system-message {
  border: medium outset ;
  padding: 1em }

div.system-message p.system-message-title {
  color: red ;
  font-weight: bold }

div.topic {
  margin: 2em }

h1.title {
  text-align: center }

h2.subtitle {
  text-align: center }

hr {
  width: 75% }

ol.simple, ul.simple {
  margin-bottom: 1em }

ol.arabic {
  list-style: decimal }

ol.loweralpha {
  list-style: lower-alpha }

ol.upperalpha {
  list-style: upper-alpha }

ol.lowerroman {
  list-style: lower-roman }

ol.upperroman {
  list-style: upper-roman }

p.attribution {
  text-align: right ;
  margin-left: 50% }

p.caption {
  font-style: italic }

p.credits {
  font-style: italic ;
  font-size: smaller }

p.label {
  white-space: nowrap }

p.rubric {
  font-weight: bold ;
  font-size: larger ;
  color: maroon ;
  text-align: center }

p.sidebar-title {
  font-family: sans-serif ;
  font-weight: bold ;
  font-size: larger }

p.sidebar-subtitle {
  font-family: sans-serif ;
  font-weight: bold }

p.topic-title {
  font-weight: bold }

pre.address {
  margin-bottom: 0 ;
  margin-top: 0 ;
  font-family: serif ;
  font-size: 100% }

pre.line-block {
  font-family: serif ;
  font-size: 100% }

pre.literal-block, pre.doctest-block {
  margin-left: 2em ;
  margin-right: 2em ;
  background-color: #eeeeee }

span.classifier {
  font-family: sans-serif ;
  font-style: oblique }

span.classifier-delimiter {
  font-family: sans-serif ;
  font-weight: bold }

span.interpreted {
  font-family: sans-serif }

span.option {
  white-space: nowrap }

span.option-argument {
  font-style: italic }

span.pre {
  white-space: pre }

span.problematic {
  color: red }

table {
  margin-top: 0.5em ;
  margin-bottom: 0.5em }

table.citation {
  border-left: solid thin gray ;
  padding-left: 0.5ex }

table.docinfo {
  margin: 2em 4em }

table.footnote {
  border-left: solid thin black ;
  padding-left: 0.5ex }

td, th {
  padding-left: 0.5em ;
  padding-right: 0.5em ;
  vertical-align: top }

th.docinfo-name, th.field-name {
  font-weight: bold ;
  text-align: left ;
  white-space: nowrap }

h1 tt, h2 tt, h3 tt, h4 tt, h5 tt, h6 tt {
  font-size: 100% }

tt {
  background-color: #eeeeee }

ul.auto-toc {
  list-style-type: none }

pre{
  BORDER-RIGHT: gray 1pt solid;
  PADDING-RIGHT: 2pt;
  BORDER-TOP: gray 1pt solid;
  DISPLAY: block;
  PADDING-LEFT: 2pt;
  PADDING-BOTTOM: 2pt;
  BORDER-LEFT: gray 1pt solid;
  MARGIN-RIGHT: 32pt;
  PADDING-TOP: 2pt;
  BORDER-BOTTOM: gray 1pt solid;
  FONT-FAMILY: "Courier New", Courier, mono;
  background-color: #EEEEEE;
}


.keyword{color: #0000FF;}
.identifier{}
.comment{font-style: italic; color: #008000;}
.special{color: #800040;}
.preprocessor{color: #3F007F;}
.string{font-style: italic; color: #666666;}
.literal{/*font-style: italic;*/ color: #000000;}
.strike { text-decoration: line-through }
.strikett { text-docoration: line-through; font-family: monospace }
.u { text-decoration: underline }
.utt { text-decoration: underline; font-family: monospace; background-color: #eeeeee; font-size: 80% }

</style>
</head>
<body>
<div class="document" id="crazy-ideas-for-the-standard-library-in-c-0x">
<h1 class="title">14 crazy ideas for the standard library in C++0x</h1>
<table class="docinfo" frame="void" rules="none">
<col class="docinfo-name" />
<col class="docinfo-content" />
<tbody valign="top">
<tr><th class="docinfo-name">Author:</th>
<td>Thorsten Ottosen</td></tr>
<tr><th class="docinfo-name">Contact:</th>
<td><a class="first last reference" href="mailto:nesotto&#64;cs.aau.dk">nesotto&#64;cs.aau.dk</a></td></tr>
<tr class="field"><th class="docinfo-name">organizations:</th><td class="field-body"><a class="reference" href="http://www.dezide.com">Dezide Aps</a> and <a class="reference" href="http://www.cs.aau.dk">Aalborg University</a></td>
</tr>
<tr><th class="docinfo-name">Date:</th>
<td>2005-08-24</td></tr>
<tr class="field"><th class="docinfo-name">Number:</th><td class="field-body">WG21/N1870 and J16/05-0130</td>
</tr>
<tr class="field"><th class="docinfo-name">Working Group:</th><td class="field-body">Evolution</td>
</tr>
</tbody>
</table>
<div class="abstract topic">
<p class="topic-title first">Abstract</p>
<p>This paper discusses 14 small ideas for enhancing
the standard library. The ideas all intend to
make the library easier to use or more efficient.</p>
</div>
<!-- ######################### -->
<!-- # Roles -->
<!-- ######################### -->
<!-- ######################### -->
<div class="contents topic" id="table-of-contents">
<p class="topic-title first"><a name="table-of-contents">Table of Contents</a></p>
<ul class="auto-toc simple">
<li><a class="reference" href="#introduction" id="id4" name="id4">1&nbsp;&nbsp;&nbsp;Introduction</a></li>
<li><a class="reference" href="#ideas-for-the-container-library" id="id5" name="id5">2&nbsp;&nbsp;&nbsp;Ideas for the container library</a><ul class="auto-toc">
<li><a class="reference" href="#add-member-resize-capacity-to-vector-and-basic-string" id="id6" name="id6">2.1&nbsp;&nbsp;&nbsp;Add member <tt class="docutils literal"><span class="pre">resize_capacity()</span></tt> to <tt class="docutils literal"><span class="pre">vector</span></tt> and <tt class="docutils literal"><span class="pre">basic_string</span></tt></a></li>
<li><a class="reference" href="#require-size-to-be-of-constant-complexity" id="id7" name="id7">2.2&nbsp;&nbsp;&nbsp;Require <tt class="docutils literal"><span class="pre">size()</span></tt> to be of constant complexity</a></li>
<li><a class="reference" href="#add-zero-function-to-standard-sequences" id="id8" name="id8">2.3&nbsp;&nbsp;&nbsp;Add <tt class="docutils literal"><span class="pre">zero()</span></tt> function to standard sequences</a></li>
<li><a class="reference" href="#add-splice-like-functions-to-node-based-containers" id="id9" name="id9">2.4&nbsp;&nbsp;&nbsp;Add splice-like functions to node-based containers</a></li>
<li><a class="reference" href="#add-set-buffer-t-size-t-to-vector-and-basic-string" id="id10" name="id10">2.5&nbsp;&nbsp;&nbsp;Add <tt class="docutils literal"><span class="pre">set_buffer(</span> <span class="pre">T*,</span> <span class="pre">size_t</span> <span class="pre">)</span></tt> to <tt class="docutils literal"><span class="pre">vector</span></tt> and <tt class="docutils literal"><span class="pre">basic_string</span></tt></a></li>
<li><a class="reference" href="#add-new-assign-member-to-all-sequences" id="id11" name="id11">2.6&nbsp;&nbsp;&nbsp;Add new <tt class="docutils literal"><span class="pre">assign()</span></tt> member to all sequences</a></li>
<li><a class="reference" href="#add-new-set-map-lookup-functions" id="id12" name="id12">2.7&nbsp;&nbsp;&nbsp;Add new set/map lookup functions</a></li>
</ul>
</li>
<li><a class="reference" href="#ideas-for-utilities" id="id13" name="id13">3&nbsp;&nbsp;&nbsp;Ideas for utilities</a><ul class="auto-toc">
<li><a class="reference" href="#add-functors-for-conversion" id="id14" name="id14">3.1&nbsp;&nbsp;&nbsp;Add functors for conversion</a></li>
<li><a class="reference" href="#add-iterator-utilities-from-boost" id="id15" name="id15">3.2&nbsp;&nbsp;&nbsp;Add iterator utilities from Boost</a></li>
<li><a class="reference" href="#add-header-std-and-tr1" id="id16" name="id16">3.3&nbsp;&nbsp;&nbsp;Add header &lt;std&gt; and &lt;tr1&gt;</a></li>
<li><a class="reference" href="#standardize-an-unspecified-bool-type" id="id17" name="id17">3.4&nbsp;&nbsp;&nbsp;Standardize an <tt class="docutils literal"><span class="pre">unspecified-bool</span></tt> type</a></li>
</ul>
</li>
<li><a class="reference" href="#ideas-for-algorithms" id="id18" name="id18">4&nbsp;&nbsp;&nbsp;Ideas for algorithms</a><ul class="auto-toc">
<li><a class="reference" href="#add-minmax-algorithms-from-boost" id="id19" name="id19">4.1&nbsp;&nbsp;&nbsp;Add <tt class="docutils literal"><span class="pre">minmax()</span></tt> algorithms from Boost</a></li>
<li><a class="reference" href="#add-a-few-statistics-algorithms-to-numeric" id="id20" name="id20">4.2&nbsp;&nbsp;&nbsp;Add a few statistics algorithms to <tt class="docutils literal"><span class="pre">&lt;numeric&gt;</span></tt></a></li>
<li><a class="reference" href="#consider-adding-additional-algorithms" id="id21" name="id21">4.3&nbsp;&nbsp;&nbsp;Consider adding additional algorithms</a></li>
</ul>
</li>
<li><a class="reference" href="#acknowledgements" id="id22" name="id22">5&nbsp;&nbsp;&nbsp;Acknowledgements</a></li>
</ul>
</div>
<div class="section" id="introduction">
<h1><a class="toc-backref" href="#id4" name="introduction">1&nbsp;&nbsp;&nbsp;Introduction</a></h1>
<p>This is a small paper about various
ideas for the standard library.</p>
<p>The ideas try to</p>
<ul class="simple">
<li>make the standard library easier to use</li>
<li>make the standard library more efficient</li>
<li>make the standard more flexible</li>
</ul>
<p>The author has often found the need
for many of the facilities suggested below.
Some has also been discussed in various
C++ newsgroups.</p>
<p>We do not provide any wording yet. The LWG 
should discuss each item and then ask
the author to provide wording if they 
like a particular idea.</p>
</div>
<div class="section" id="ideas-for-the-container-library">
<h1><a class="toc-backref" href="#id5" name="ideas-for-the-container-library">2&nbsp;&nbsp;&nbsp;Ideas for the container library</a></h1>
<div class="section" id="add-member-resize-capacity-to-vector-and-basic-string">
<h2><a class="toc-backref" href="#id6" name="add-member-resize-capacity-to-vector-and-basic-string">2.1&nbsp;&nbsp;&nbsp;Add member <tt class="docutils literal docutils literal"><span class="pre">resize_capacity()</span></tt> to <tt class="docutils literal docutils literal"><span class="pre">vector</span></tt> and <tt class="docutils literal docutils literal"><span class="pre">basic_string</span></tt></a></h2>
<p><strong>Motivation</strong>:</p>
<ul>
<li><p class="first">Remove the need for &quot;the swap trick&quot; to trim excess capacity
in a vector or a string. We can now just say</p>
<pre class="literal-block">
vector&lt;T&gt; vec = ...;
...
vec.resize_capacity( vec.size() );
</pre>
</li>
<li><p class="first">Provide explicit control over the buffer size.</p>
</li>
</ul>
<p><strong>Remarks</strong>:</p>
<p>&quot;The swap trick&quot; is discussed in Effective STL, item 17. 
Its still a bit of a hack compared to genuine support
for controling the capacity.</p>
</div>
<div class="section" id="require-size-to-be-of-constant-complexity">
<h2><a class="toc-backref" href="#id7" name="require-size-to-be-of-constant-complexity">2.2&nbsp;&nbsp;&nbsp;Require <tt class="docutils literal docutils literal"><span class="pre">size()</span></tt> to be of constant complexity</a></h2>
<p>Section 23.1/5 allows a container to implement <tt class="docutils literal"><span class="pre">size()</span></tt>
with linear complexity. This flexibility can sometimes
be used by implementers. However, it is doubtful if this
is for the benefit of the users.</p>
<p>We therefore suggest that it <tt class="docutils literal"><span class="pre">size()</span></tt> is required to 
be of constant complexity.</p>
<p><strong>Motivation</strong>:</p>
<ul class="simple">
<li>If users forget to put the computation of the container's
size outside the loop, but rather call <tt class="docutils literal"><span class="pre">size()</span></tt> repeatedly,
a straight-forward O(n) loop becomes O(n^2).</li>
</ul>
<p><strong>Remarks</strong>:</p>
<p>The rest of the functions under Note A might be 
re-considered too.</p>
</div>
<div class="section" id="add-zero-function-to-standard-sequences">
<h2><a class="toc-backref" href="#id8" name="add-zero-function-to-standard-sequences">2.3&nbsp;&nbsp;&nbsp;Add <tt class="docutils literal docutils literal"><span class="pre">zero()</span></tt> function to standard sequences</a></h2>
<p>With the like advent of a new <tt class="docutils literal"><span class="pre">auto</span></tt>,
it become much easier to specify iterators
inside loops</p>
<pre class="literal-block">
for( auto i = cont.begin(), e = cont.end(); ... )
</pre>
<p>When two variables are initialized in one statement, it
is required that they are deduced to be the same type.
This raises a problem with sequences because</p>
<pre class="literal-block">
for( auto i = 0, s = cont.size(); i != s; ... )
</pre>
<p>is not legal. We have to do the rather awkward</p>
<pre class="literal-block">
for( auto i = decltype(cont.size())(0), s = cont.size(); i != s; ... )
</pre>
<p>Therefore we suggest to add a helper member function
to sequences that return a zero of the correct size type:</p>
<pre class="literal-block">
size_type zero() const { return 0; }
</pre>
<p>which allows us to write</p>
<pre class="literal-block">
for( auto i = cont.zero(), s = cont.size(); i != s; ... )
</pre>
</div>
<div class="section" id="add-splice-like-functions-to-node-based-containers">
<h2><a class="toc-backref" href="#id9" name="add-splice-like-functions-to-node-based-containers">2.4&nbsp;&nbsp;&nbsp;Add splice-like functions to node-based containers</a></h2>
<p>Move-semantics is going to give the standard library
a well-deserved speed enhancement. However, 
for node-based containers we should be 
able to move individual nodes between containers
of the same type. This essentially corresponds
to the <tt class="docutils literal"><span class="pre">splice()</span></tt> members in <tt class="docutils literal"><span class="pre">list</span></tt>.</p>
<p>The speedup could be enormous for 
certain operations on node-based containers.</p>
<p>Therefore we suggest that the following
members are added to <tt class="docutils literal"><span class="pre">map</span></tt>, <tt class="docutils literal"><span class="pre">multimap</span></tt>,
<tt class="docutils literal"><span class="pre">set</span></tt> and <tt class="docutils literal"><span class="pre">multiset</span></tt></p>
<pre class="literal-block">
<em>container</em>
{
    ...
    void splice( <em>container&amp;&amp;</em> );
    void splice( iterator, iterator, <em>container&amp;</em> );
    void splice( iterator, <em>container&amp;</em> ); 
} 
</pre>
<p>which is guaranteed to reuse the internal
nodes.</p>
<p><strong>Remarks</strong>:</p>
<ul class="simple">
<li>Would it be possible to do this for <tt class="docutils literal"><span class="pre">deque</span></tt> to?</li>
<li>Can this work even if the allocators are different?</li>
</ul>
</div>
<div class="section" id="add-set-buffer-t-size-t-to-vector-and-basic-string">
<h2><a class="toc-backref" href="#id10" name="add-set-buffer-t-size-t-to-vector-and-basic-string">2.5&nbsp;&nbsp;&nbsp;Add <tt class="docutils literal docutils literal"><span class="pre">set_buffer(</span> <span class="pre">T*,</span> <span class="pre">size_t</span> <span class="pre">)</span></tt> to <tt class="docutils literal docutils literal"><span class="pre">vector</span></tt> and <tt class="docutils literal docutils literal"><span class="pre">basic_string</span></tt></a></h2>
<p>It would often be convenient if continuous-storage containers
could take ownership of a heap-allocated array. This is
important in low level-code when either</p>
<ul class="simple">
<li>one have a heap-allocated array already</li>
<li>one does not want to initialize the buffer,
because it must be completely overwritten anyway</li>
</ul>
<p><strong>Remarks</strong>:</p>
<ul class="simple">
<li>Can non-default allocators be a problem?</li>
</ul>
</div>
<div class="section" id="add-new-assign-member-to-all-sequences">
<h2><a class="toc-backref" href="#id11" name="add-new-assign-member-to-all-sequences">2.6&nbsp;&nbsp;&nbsp;Add new <tt class="docutils literal docutils literal"><span class="pre">assign()</span></tt> member to all sequences</a></h2>
<p>The following member:</p>
<pre class="literal-block">
template &lt;class T, ...&gt;
class sequence 
{
  // call 'f' 'n' times
  // 'f()' takes a reference to an element to initialize
  template&lt; class F &gt; assign( size_type n, F f );
  ..
}; 
</pre>
<p>has the following advantages:</p>
<ol class="arabic simple">
<li>it allows users to fill the sequence using a functor</li>
</ol>
<!--  -->
<ol class="arabic simple" start="2">
<li>it allows advanced users to forgo default initialization by
supplying an empty functor</li>
</ol>
</div>
<div class="section" id="add-new-set-map-lookup-functions">
<h2><a class="toc-backref" href="#id12" name="add-new-set-map-lookup-functions">2.7&nbsp;&nbsp;&nbsp;Add new set/map lookup functions</a></h2>
<p>Consider a simple word-counting example:</p>
<pre class="literal-block">
map&lt;string,int&gt; m;
...
for(...)
    m[ str(b,e) ]++;
</pre>
<p>We need to construct a temporary string just to
check if we should increment the word count---and 
we then throw this temporary away. This
can be very expensive when the key is a heavy object.</p>
<p>Therefore we propose that the following new lookup
functions are added to <tt class="docutils literal"><span class="pre">map</span></tt> and <tt class="docutils literal"><span class="pre">unordered_map</span></tt></p>
<pre class="literal-block">
template&lt; class ComparableToKey &gt;
value_type&amp; lazy_insert( const ComparableToKey&amp; );
</pre>
<p>If the object is not found, it needs to be converted.
This should be done via a call to</p>
<pre class="literal-block">
template&lt; class T, class U &gt;
T convert_to( const U&amp; r )
{
    // default implementation should use iterator-range
    // construction
}
</pre>
<p>Alternatively we could rely on implicit conversion by default   
(the important thing being that we can customize it).</p>
<p>For <tt class="docutils literal"><span class="pre">set</span></tt> and <tt class="docutils literal"><span class="pre">unordered_set</span></tt> we should add</p>
<pre class="literal-block">
template&lt; class ComparableToKey &gt;
std::pair&lt;iterator,bool&gt; lazy_insert( const ComparableToKey&amp; );
</pre>
<p>Also, in a similar manner, we could add the following members
to <tt class="docutils literal"><span class="pre">map</span></tt>, <tt class="docutils literal"><span class="pre">unordered_map</span></tt>, <tt class="docutils literal"><span class="pre">set</span></tt> and <tt class="docutils literal"><span class="pre">unordered_set</span></tt>:</p>
<pre class="literal-block">
template&lt; class ComparableToKey &gt;
iterator lazy_find( const ComparableToKey&amp; r );

template&lt; class ComparableToKey &gt;
size_type lazy_count( const ComparableToKey&amp; r );
</pre>
<p>Perhaps even extensions to <tt class="docutils literal"><span class="pre">lower_bound()</span></tt> etc. should be allowed.</p>
</div>
</div>
<div class="section" id="ideas-for-utilities">
<h1><a class="toc-backref" href="#id13" name="ideas-for-utilities">3&nbsp;&nbsp;&nbsp;Ideas for utilities</a></h1>
<div class="section" id="add-functors-for-conversion">
<h2><a class="toc-backref" href="#id14" name="add-functors-for-conversion">3.1&nbsp;&nbsp;&nbsp;Add functors for conversion</a></h2>
<p>Conversion is a very important
correctness aspects of the language. The language is not
too good in this respect, although good compilers do
give warnings.</p>
<p><strong>Motivation</strong>:</p>
<ul class="simple">
<li>If there is no easy way to do a certain conversion,
users tend to not do it.</li>
<li>It is not uncommon that users simply turn conversion
warnings off. Conversions need to be painless.</li>
<li>We need these functors when applying a 
conversion to a sequence (perhaps via
some kind of transform iterator).</li>
</ul>
<p><strong>Remarks</strong>:</p>
<p>The following functors are suggested:</p>
<pre class="literal-block">
template&lt; class T &gt;
struct static_cast_to
{
    template&lt; class U &gt;
    T operator()( U u ) const
    {
        return static_cast&lt;T&gt;(u);
    }
};

template&lt; class T &gt;
struct dynamic_cast_to
{
    template&lt; class U &gt;
    T operator()( U u ) const
    {
        return dynamic_cast&lt;T&gt;(u);
    }
};

template&lt; class T &gt;
struct const_cast_to
{
    template&lt; class U &gt;
    T operator()( U u ) const
    {
        return const_cast&lt;T&gt;(u);
    }
};

template&lt; class T &gt;
struct reinterpret_cast_to
{
    template&lt; class U &gt;
    T operator()( U u ) const
    {
        return reinterprest_cast&lt;T&gt;(u);
    }
};
</pre>
<p>If we are lucky enough to get a <tt class="docutils literal"><span class="pre">numeric_cast&lt;T&gt;</span></tt>
proposal we should also add</p>
<pre class="literal-block">
template&lt; class T &gt;
struct numeric_cast_to
{
    template&lt; class U &gt;
    T operator()( U u ) const
    {
        return numeric_cast&lt;T&gt;(u);
    }
};
</pre>
<p><strong>Discussion</strong>:</p>
<p>Are the arguments of <tt class="docutils literal"><span class="pre">operator()()</span></tt> properly specified?</p>
</div>
<div class="section" id="add-iterator-utilities-from-boost">
<h2><a class="toc-backref" href="#id15" name="add-iterator-utilities-from-boost">3.2&nbsp;&nbsp;&nbsp;Add iterator utilities from Boost</a></h2>
<p>The iterator library could benefit
from these handy utilities from <a class="reference" href="http://www.boost.org/libs/utility/utility.htm#functions_next_prior">Boost.Util</a></p>
<blockquote>
<pre class="literal-block">
template &lt;class T&gt;
T next(T x) { return ++x; }

template &lt;class T, class Distance&gt;
T next(T x, Distance n)
{
    std::advance(x, n);
    return x;
}

template &lt;class T&gt;
T prior(T x) { return --x; }

template &lt;class T, class Distance&gt;
T prior(T x, Distance n)
{
    std::advance(x, -n);
    return x;
}
</pre>
</blockquote>
<p><strong>Motivation</strong>:</p>
<ul class="simple">
<li>the utilities are very handy</li>
<li>the utilities are often requested by users</li>
<li>the utilities are very simple</li>
</ul>
<!-- Provide stronger exception-safety guarantees for ``vector``
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Section 23.2.4.3 remarks 

    If an exception is thrown other than
    by the copy constructor or assignment operator of T there are no effects.

This affects the implementation of ``assign()`` because it is 
specified in terms of ``insert()``. However, ``assign()`` may be
implemented as ::

    void assign( Iterator b, Iterator e )
    {
        vector<T> temp(b,e);
        swap(temp);
    }
    
leading to the strong-guarantee of exception-safety. Similarly,
the other version of assign may be changed too. -->
</div>
<div class="section" id="add-header-std-and-tr1">
<h2><a class="toc-backref" href="#id16" name="add-header-std-and-tr1">3.3&nbsp;&nbsp;&nbsp;Add header &lt;std&gt; and &lt;tr1&gt;</a></h2>
<p>This would enable better support for precompiled headers.
Even with modules in C++0x, this will be
easy to do for many years until modules are widely available.</p>
<p>It is also considerably easier to teach
newbies that they simply include <tt class="docutils literal"><span class="pre">&lt;std&gt;</span></tt>.</p>
</div>
<div class="section" id="standardize-an-unspecified-bool-type">
<h2><a class="toc-backref" href="#id17" name="standardize-an-unspecified-bool-type">3.4&nbsp;&nbsp;&nbsp;Standardize an <tt class="docutils literal docutils literal"><span class="pre">unspecified-bool</span></tt> type</a></h2>
<p>An unknown function-pointer is often used when
eg. smart pointers provide an implicit conversion
to <tt class="docutils literal"><span class="pre">bool</span></tt>, only in a more restrictive form that
is only intended to be used in an if-statement:</p>
<pre class="literal-block">
smart_ptr&lt;T&gt; p = ...;
...
if( p )
{
    ...
}
</pre>
<p>The idiom is so common with library writers that we should
standardize the type, for example</p>
<pre class="literal-block">
namespace std
{
    typedef <em>unspecified-type</em> restricted_bool;
    const restricted_bool      restricted_true;
    const restricted_bool      restricted_false;
}
</pre>
</div>
</div>
<div class="section" id="ideas-for-algorithms">
<h1><a class="toc-backref" href="#id18" name="ideas-for-algorithms">4&nbsp;&nbsp;&nbsp;Ideas for algorithms</a></h1>
<div class="section" id="add-minmax-algorithms-from-boost">
<h2><a class="toc-backref" href="#id19" name="add-minmax-algorithms-from-boost">4.1&nbsp;&nbsp;&nbsp;Add <tt class="docutils literal docutils literal"><span class="pre">minmax()</span></tt> algorithms from Boost</a></h2>
<p>For some time these utilities have been
part of <a class="reference" href="http://www.boost.org/libs/algorithm/minmax/index.html#synopsis">boost</a>.</p>
<p>The algorithms are easy to specify and complement
<tt class="docutils literal"><span class="pre">min()</span></tt> and <tt class="docutils literal"><span class="pre">max()</span></tt> algorithms very well:</p>
<pre class="literal-block">
template &lt;class T&gt;
tuple&lt;T const&amp;, T const&amp;&gt; &gt;
minmax(const T&amp; a, const T&amp; b);

template &lt;class T, class BinaryPredicate&gt;
tuple&lt;T const&amp;, T const&amp;&gt; &gt;
minmax(const T&amp; a, const T&amp; b, BinaryPredicate comp);

template &lt;class ForwardIterator&gt;
std::pair&lt;ForwardIterator,ForwardIterator&gt;
minmax_element(ForwardIterator first, ForwardIterator last);

template &lt;class ForwardIterator, class BinaryPredicate&gt;
std::pair&lt;ForwardIterator,ForwardIterator&gt;
minmax_element(ForwardIterator first, ForwardIterator last,
               BinaryPredicate comp);
</pre>
<!-- **Remarks**: -->
<!-- - the argument types of ``minmax()`` 
should probably be as ``std::max()``. -->
</div>
<div class="section" id="add-a-few-statistics-algorithms-to-numeric">
<h2><a class="toc-backref" href="#id20" name="add-a-few-statistics-algorithms-to-numeric">4.2&nbsp;&nbsp;&nbsp;Add a few statistics algorithms to <tt class="docutils literal docutils literal"><span class="pre">&lt;numeric&gt;</span></tt></a></h2>
<p>A few often use algorithms would be nice
to see in <tt class="docutils literal"><span class="pre">&lt;numeric&gt;</span></tt>:</p>
<pre class="literal-block">
template&lt; class T, class InputIterator &gt;
T mean( InputIterator first, InputIterator last );

template&lt; class T, class InputIterator &gt;
T variance( InputIterator first, InputIterator last );

template&lt; class T, class InputIterator &gt;
T std_deviation( InputIterator first, InputIterator last );

template&lt; class T, class InputIterator, class InputIterator2 &gt;
T covariance( InputIterator first, InputIterator last,
              InputIterator2 first2, InputIterator2 last2 );

template&lt; class T, class InputIterator, class InputIterator2 &gt;
T
correlation_coefficient( InputIterator first, InputIterator last,
                         InputIterator2 first2, InputIterator2 last2 );

template&lt; class T, class InputIterator, class InputIterator2 &gt;
tuple&lt;T,T,T&gt; 
least_square_line( InputIterator first, InputIterator last, 
                   InputIterator2 first2, InputIterator2 last2 );

template&lt; class T, class InputIterator, class InputIterator2 &gt;
tuple&lt;T,T&gt;
least_square_line_without_correlation( InputIterator first, InputIterator last,
                                       InputIterator2 first2, InputIterator2 last2 );
                                       
template&lt; class T &gt;
T factorial( T n );

template&lt; class T &gt;
T nPr( T n, T r );

template&lt; class T &gt;
T nCr( T n, T r );

template&lt; class T, class Integer &gt;
T binomial( T p, Integer n, Integer r );

template&lt; class T, class Integer &gt;
T negative_binomial( T p, Integer n, Integer r );

template&lt; class T, class Integer &gt;
T poisson( T lambda, Integer r );

template&lt; class T, class Integer &gt;
T hypergeometric( Integer n, Integer r,
                  Integer blue, Integer red );

template&lt; class T, class Integer &gt;
T geometric( T p, Integer r );
</pre>
<p><strong>Motivation</strong>:</p>
<ul class="simple">
<li>many of these functions are frequently used</li>
<li>many of these functions are nice for beginners
that need to make small games or be taught introductive
statistics (this will benefit C++ as a language to start
introductive programming in).</li>
<li>functions like <tt class="docutils literal"><span class="pre">mean()</span></tt>, <tt class="docutils literal"><span class="pre">std_deviation()</span></tt> and <tt class="docutils literal"><span class="pre">variance()</span></tt>
are fairly easy to implement, but if high-quality single-pass
algorithm takes considerable more time. As such, the library
algorithm will be guaranteed to be optimal.</li>
<li>an implementation of all this may be found in the boost sandbox
under the directory <tt class="docutils literal"><span class="pre">stat</span></tt>.</li>
</ul>
</div>
<div class="section" id="consider-adding-additional-algorithms">
<h2><a class="toc-backref" href="#id21" name="consider-adding-additional-algorithms">4.3&nbsp;&nbsp;&nbsp;Consider adding additional algorithms</a></h2>
<p>Consider adding some of the 
algorithms discussed on the <a class="reference" href="http://www.crystalclearsoftware.com/cgi-bin/boost_wiki/wiki.pl?STLAlgorithmExtensions">Boost Wiki</a></p>
<p>In particular consider</p>
<ul class="simple">
<li><tt class="docutils literal"><span class="pre">is_sorted()</span></tt></li>
<li><tt class="docutils literal"><span class="pre">nonunique_copy()</span></tt></li>
</ul>
</div>
</div>
<div class="section" id="acknowledgements">
<h1><a class="toc-backref" href="#id22" name="acknowledgements">5&nbsp;&nbsp;&nbsp;Acknowledgements</a></h1>
<p>The author has presented some ideas directly or indirectly
taken from or inspired by the 
following people (at least):</p>
<ul class="simple">
<li>David Abrahams</li>
<li>Howard Hinnant</li>
<li>Scott Meyers</li>
<li>Daniel Walker</li>
<li>Hervé Brönnimann</li>
<li>Michael Goldshteyn</li>
</ul>
<p>Also thanks to the people on <tt class="docutils literal"><span class="pre">comp.std.c++</span></tt> for their
suggestions. I think some of the 
above ideas will make some of the suggestions much
easier to write efficiently outside the standard.</p>
<!-- ############################################### -->
<!-- ################## Links ###################### -->
<!-- ############################################### -->
<!-- ========= -->
<!-- Footnotes -->
<!-- ========= -->
<!-- ############################################### -->
<!-- ################## Footnotes ################## -->
<!-- ############################################### -->
</div>
</div>
</body>
</html>
