<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 561: inserter overly generic</title>
<meta property="og:title" content="Issue 561: inserter overly generic">
<meta property="og:description" content="C++ library issue. Status: CD1">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue561.html">
<meta property="og:type" content="website">
<meta property="og:image" content="http://cplusplus.github.io/LWG/images/cpp_logo.png">
<meta property="og:image:alt" content="C++ logo">
<style>
  p {text-align:justify}
  li {text-align:justify}
  pre code.backtick::before { content: "`" }
  pre code.backtick::after { content: "`" }
  blockquote.note
  {
    background-color:#E0E0E0;
    padding-left: 15px;
    padding-right: 15px;
    padding-top: 1px;
    padding-bottom: 1px;
  }
  ins {background-color:#A0FFA0}
  del {background-color:#FFA0A0}
  table.issues-index { border: 1px solid; border-collapse: collapse; }
  table.issues-index th { text-align: center; padding: 4px; border: 1px solid; }
  table.issues-index td { padding: 4px; border: 1px solid; }
  table.issues-index td:nth-child(1) { text-align: right; }
  table.issues-index td:nth-child(2) { text-align: left; }
  table.issues-index td:nth-child(3) { text-align: left; }
  table.issues-index td:nth-child(4) { text-align: left; }
  table.issues-index td:nth-child(5) { text-align: center; }
  table.issues-index td:nth-child(6) { text-align: center; }
  table.issues-index td:nth-child(7) { text-align: left; }
  table.issues-index td:nth-child(5) span.no-pr { color: red; }
  @media (prefers-color-scheme: dark) {
     html {
        color: #ddd;
        background-color: black;
     }
     ins {
        background-color: #225522
     }
     del {
        background-color: #662222
     }
     a {
        color: #6af
     }
     a:visited {
        color: #6af
     }
     blockquote.note
     {
        background-color: rgba(255, 255, 255, .10)
     }
  }
</style>
</head>
<body>
<hr>
<p><em>This page is a snapshot from the LWG issues list, see the <a href="lwg-active.html">Library Active Issues List</a> for more information and the meaning of <a href="lwg-active.html#CD1">CD1</a> status.</em></p>
<h3 id="561"><a href="lwg-defects.html#561">561</a>. inserter overly generic</h3>
<p><b>Section:</b> 24.5.2.4.3 <a href="https://wg21.link/inserter">[inserter]</a> <b>Status:</b> <a href="lwg-active.html#CD1">CD1</a>
 <b>Submitter:</b> Howard Hinnant <b>Opened:</b> 2006-02-21 <b>Last modified:</b> 2016-01-28</p>
<p><b>Priority: </b>Not Prioritized
</p>
<p><b>View all issues with</b> <a href="lwg-status.html#CD1">CD1</a> status.</p>
<p><b>Discussion:</b></p>
<p>
The declaration of <code>std::inserter</code> is:
</p>

<blockquote><pre>
template &lt;class Container, class Iterator&gt;
insert_iterator&lt;Container&gt;
inserter(Container&amp; x, Iterator i);
</pre></blockquote>

<p>
The template parameter <code>Iterator</code> in this function is completely unrelated
to the template parameter <code>Container</code> when it doesn't need to be.  This
causes the code to be overly generic.  That is, any type at all can be deduced
as <code>Iterator</code>, whether or not it makes sense.  Now the same is true of
<code>Container</code>.  However, for every free (unconstrained) template parameter
one has in a signature, the opportunity for a mistaken binding grows geometrically.
</p>

<p>
It would be much better if <code>inserter</code> had the following signature instead:
</p>

<blockquote><pre>
template &lt;class Container&gt;
insert_iterator&lt;Container&gt;
inserter(Container&amp; x, typename Container::iterator i);
</pre></blockquote>

<p>
Now there is only one free template parameter.  And the second argument to
<code>inserter</code> must be implicitly convertible to the container's iterator,
else the call will not be a viable overload (allowing other functions in the
overload set to take precedence).  Furthermore, the first parameter must have a
nested type named <code>iterator</code>, or again the binding to <code>std::inserter</code>
is not viable.  Contrast this with the current situation
where any type can bind to <code>Container</code> or <code>Iterator</code> and those
types need not be anything closely related to containers or iterators.
</p>

<p>
This can adversely impact well written code.  Consider:
</p>

<blockquote><pre>
#include &lt;iterator&gt;
#include &lt;string&gt;

namespace my
{

template &lt;class String&gt;
struct my_type {};

struct my_container
{
template &lt;class String&gt;
void push_back(const my_type&lt;String&gt;&amp;);
};

template &lt;class String&gt;
void inserter(const my_type&lt;String&gt;&amp; m, my_container&amp; c) {c.push_back(m);}

}  // my

int main()
{
    my::my_container c;
    my::my_type&lt;std::string&gt; m;
    inserter(m, c);
}
</pre></blockquote>

<p>
Today this code fails because the call to <code>inserter</code> binds to
<code>std::inserter</code> instead of to <code>my::inserter</code>.  However with the
proposed change <code>std::inserter</code> will no longer be a viable function which
leaves only <code>my::inserter</code> in the overload resolution set.  Everything
works as the client intends.
</p>

<p>
To make matters a little more insidious, the above example works today if you
simply change the first argument to an rvalue:
</p>

<blockquote><pre>
    inserter(my::my_type(), c);
</pre></blockquote>

<p>
It will also work if instantiated with some string type other than
<code>std::string</code> (or any other <code>std</code> type).  It will also work if
<code>&lt;iterator&gt;</code> happens to not get included.
</p>

<p>
And it will fail again for such inocuous reaons as <code>my_type</code> or
<code>my_container</code> privately deriving from any <code>std</code> type.
</p>

<p>
It seems unfortunate that such simple changes in the client's code can result
in such radically differing behavior.
</p>



<p id="res-561"><b>Proposed resolution:</b></p>
<p>
Change 24.2:
</p>

<blockquote><p>
<b>24.2 Header</b> <code>&lt;iterator&gt;</code> <b>synopsis</b>
</p>
<blockquote><pre>
...
template &lt;class Container<del>, class Iterator</del>&gt;
   insert_iterator&lt;Container&gt; inserter(Container&amp; x, <del>Iterator</del> <ins>typename Container::iterator</ins> i);
...
</pre></blockquote>
</blockquote>

<p>
Change 24.4.2.5:
</p>

<blockquote><p>
<b>24.4.2.5 Class template</b> <code>insert_iterator</code></p>
<blockquote><pre>
...
template &lt;class Container<del>, class Iterator</del>&gt;
   insert_iterator&lt;Container&gt; inserter(Container&amp; x, <del>Iterator</del> <ins>typename Container::iterator</ins> i);
...
</pre></blockquote>
</blockquote>

<p>
Change 24.4.2.6.5:
</p>

<blockquote>
<p>
<b>24.4.2.6.5</b> <code>inserter</code>
</p>
<pre>
template &lt;class Container<del>, class Inserter</del>&gt;
   insert_iterator&lt;Container&gt; inserter(Container&amp; x, <del>Inserter</del> <ins>typename Container::iterator</ins> i);
</pre>
<blockquote><p>
-1- <i>Returns:</i> <code>insert_iterator&lt;Container&gt;(x,<del>typename Container::iterator(</del>i<del>)</del>)</code>.
</p></blockquote>
</blockquote>



<p><i>[
Kona (2007): This issue will probably be addressed as a part of the concepts overhaul of the library anyway, but the proposed resolution is correct in the absence of concepts. 
Proposed Disposition: Ready
]</i></p>





</body>
</html>
