<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 2268: Setting a default argument in the declaration of a member function assign of std::basic_string</title>
<meta property="og:title" content="Issue 2268: Setting a default argument in the declaration of a member function assign of std::basic_string">
<meta property="og:description" content="C++ library issue. Status: C++14">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue2268.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#C++14">C++14</a> status.</em></p>
<h3 id="2268"><a href="lwg-defects.html#2268">2268</a>. Setting a default argument in the declaration of a member function <code>assign</code> of <code>std::basic_string</code></h3>
<p><b>Section:</b> 27.4.3 <a href="https://wg21.link/basic.string">[basic.string]</a> <b>Status:</b> <a href="lwg-active.html#C++14">C++14</a>
 <b>Submitter:</b> Vladimir Grigoriev <b>Opened:</b> 2013-06-26 <b>Last modified:</b> 2016-11-12</p>
<p><b>Priority: </b>Not Prioritized
</p>
<p><b>View other</b> <a href="lwg-index-open.html#basic.string">active issues</a> in [basic.string].</p>
<p><b>View all other</b> <a href="lwg-index.html#basic.string">issues</a> in [basic.string].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#C++14">C++14</a> status.</p>
<p><b>Discussion:</b></p>
<p>
Constructors and member functions <code>assign</code> of class <code>std::basic_string</code> have one to one relation (except the 
explicit constructor that creates an empty string). The following list shows this relation:
</p>

<blockquote><pre>
explicit basic_string(const Allocator&amp; a = Allocator());

basic_string(const basic_string&amp; str);
basic_string&amp; assign(const basic_string&amp; str);

basic_string(basic_string&amp;&amp; str) noexcept;
basic_string&amp; assign(basic_string&amp;&amp; str) noexcept;

basic_string(const basic_string&amp; str, size_type pos, size_type n = npos,
  const Allocator&amp; a = Allocator());
basic_string&amp; assign(const basic_string&amp; str, size_type pos,
  size_type n);

basic_string(const charT* s,
  size_type n, const Allocator&amp; a = Allocator());
basic_string&amp; assign(const charT* s, size_type n);

basic_string(const charT* s, const Allocator&amp; a = Allocator());
basic_string&amp; assign(const charT* s);

basic_string(size_type n, charT c, const Allocator&amp; a = Allocator());
basic_string&amp; assign(size_type n, charT c);

template&lt;class InputIterator&gt;
basic_string(InputIterator begin, InputIterator end,
  const Allocator&amp; a = Allocator());
template&lt;class InputIterator&gt;
basic_string&amp; assign(InputIterator first, InputIterator last);

basic_string(initializer_list&lt;charT&gt;, const Allocator&amp; = Allocator());
basic_string&amp; assign(initializer_list&lt;charT&gt;);
</pre></blockquote>

<p>
So in fact any creating of an object of type <code>std::basic_string</code> using any of the above constructors 
except the explicit constructor can be substituted for creating a (possibly non-empty) string and 
then applying to it the corresponding method assign.
<p/>
For example these two code snippets give the same result:
</p>

<blockquote><pre>
std::string s("Hello World");
</pre></blockquote>

<p>
and
</p>

<blockquote><pre>
std::string s;
s.assign("Hello World");
</pre></blockquote>

<p>
However there is one exception that has no a logical support. It is the pair of the following constructor and member function 
<code>assign</code>
</p>

<blockquote><pre>
basic_string(const basic_string&amp; str, size_type pos, size_type n = npos,
             const Allocator&amp; a = Allocator());

basic_string&amp; assign(const basic_string&amp; str, size_type pos, size_type n);
</pre></blockquote>

<p>
The third parameter of the constructor has a default argument while in the <code>assign</code> function it is absent. So it is impossible 
one to one to substitute the following code snippet
</p>

<blockquote><pre>
std::string s("Hello World");
std::string t(s, 6);
</pre></blockquote>

<p>
by
</p>

<blockquote><pre>
std::string s("Hello World");
std::string t;
t.assign(s, 6); // error: no such function
</pre></blockquote>

<p>
To get an equivalent result using the <code>assign</code> function the programmer has to complicate the code that is error-prone
</p>

<blockquote><pre>
std::string s("Hello World");
std::string t;
t.assign(s, 6, s.size() - 6); 
</pre></blockquote>

<p>
To fix that, the declaration of the member function <code>assign</code> should be changed in such a way that its declaration 
would be fully compatible with the declaration of the corresponding constructor, that is to specify the same default argument 
for the third parameter of the <code>assign</code>.
</p>

<p>
The <code>assign</code> function is not the only function that requires to be revised. 
<p/>
Now let include in the list of pairs constructor-assign with the modified method <code>assign</code> one more member function 
<code>append</code>. We will get:
</p>

<blockquote><pre>
explicit basic_string(const Allocator&amp; a = Allocator());

basic_string(const basic_string&amp; str);
basic_string&amp; assign(const basic_string&amp; str);
<span style="color:#009900;font-weight:bold">basic_string&amp; append(const basic_string&amp; str);</span>

basic_string(basic_string&amp;&amp; str) noexcept;
basic_string&amp; assign(basic_string&amp;&amp; str) noexcept;

basic_string(const basic_string&amp; str, size_type pos, size_type n = npos,
  const Allocator&amp; a = Allocator());
basic_string&amp; assign(const basic_string&amp; str, size_type pos,
  size_type n);
<span style="color:#009900;font-weight:bold">basic_string&amp; append(const basic_string&amp; str, size_type pos,
  size_type n);</span>

basic_string(const charT* s,
  size_type n, const Allocator&amp; a = Allocator());
basic_string&amp; assign(const charT* s, size_type n);
<span style="color:#009900;font-weight:bold">basic_string&amp; append(const charT* s, size_type n);</span>

basic_string(const charT* s, const Allocator&amp; a = Allocator());
basic_string&amp; assign(const charT* s);
<span style="color:#009900;font-weight:bold">basic_string&amp; append(const charT* s);</span>

basic_string(size_type n, charT c, const Allocator&amp; a = Allocator());
basic_string&amp; assign(size_type n, charT c);
<span style="color:#009900;font-weight:bold">basic_string&amp; append(size_type n, charT c);</span>

template&lt;class InputIterator&gt;
basic_string(InputIterator begin, InputIterator end,
  const Allocator&amp; a = Allocator());
template&lt;class InputIterator&gt;
basic_string&amp; assign(InputIterator first, InputIterator last);
<span style="color:#009900;font-weight:bold">template&lt;class InputIterator&gt;
basic_string&amp; append(InputIterator first, InputIterator last);</span>

basic_string(initializer_list&lt;charT&gt;, const Allocator&amp; = Allocator());
basic_string&amp; assign(initializer_list&lt;charT&gt;);
<span style="color:#009900;font-weight:bold">basic_string&amp; append(initializer_list&lt;charT&gt;);</span>
</pre></blockquote>

<p>As it seen from this record:</p>

<blockquote><pre>
basic_string(const basic_string&amp; str, size_type pos, size_type n = npos,
  const Allocator&amp; a = Allocator());
basic_string&amp; assign(const basic_string&amp; str, size_type pos,
  size_type n);
<span style="color:#009900;font-weight:bold">basic_string&amp; append(const basic_string&amp; str, size_type pos,
  size_type n);</span>
</pre></blockquote>

<p>
it is obvious that the function <code>append</code> also should have the default argument that is that it should be declared as:
</p>

<blockquote><pre>
basic_string&amp; append(const basic_string&amp; str, size_type pos,
  size_type n <ins>= npos</ins>);
</pre></blockquote>

<p>
In fact there is no a great difference in using <code>assign</code> or <code>append</code> especially when the string is empty:
</p>

<blockquote><pre>
std::string s("Hello World");
std::string t;
t.assign(s, 6);
 
std::string s("Hello World");
std::string t;
t.append(s, 6);
</pre></blockquote>

<p>
In both cases the result will be the same. So the <code>assign</code> and <code>append</code> will be interchangeable from the point 
of view of used arguments.
<p/>
There are another three member functions in class <code>std::basic_string</code> that could be brought in conformity with considered above functions.  
They are member functions <code>insert</code>, <code>replace</code>, and <code>compare</code>.
<p/>
So it is suggested to substitute the following declarations of <code>insert</code>, <code>replace</code>, and <code>compare</code>:
</p>

<blockquote><pre>
basic_string&amp; insert(size_type pos1, const basic_string&amp; str,
  size_type pos2, size_type n);

basic_string&amp; replace(size_type pos1, size_type n1,
  const basic_string&amp; str, size_type pos2, size_type n2);

int compare(size_type pos1, size_type n1,
  const basic_string&amp; str, size_type pos2, size_type n2) const;
</pre></blockquote>

<p>
by the declarations:
</p>

<blockquote><pre>
basic_string&amp; insert(size_type pos1, const basic_string&amp; str,
  size_type pos2, size_type n <ins>= npos</ins>);

basic_string&amp; replace(size_type pos1, size_type n1,
  const basic_string&amp; str, size_type pos2, size_type n2 <ins>= npos</ins>);

int compare(size_type pos1, size_type n1,
  const basic_string&amp; str, size_type pos2, size_type n2 <ins>= npos</ins>) const;
</pre></blockquote>

<p><i>[2013-09 Chicago]</i></p>

<p>
Howard: Are we positive this won't conflict with any other overloads? 
<p/>
They all appear to be unambiguous. 
<p/>
Alisdair: Ok, move to Ready. 
</p>


<p id="res-2268"><b>Proposed resolution:</b></p>

<ol>
<li><p>Change class template <code>basic_string</code> synopsis, 27.4.3 <a href="https://wg21.link/basic.string">[basic.string]</a> p5, as indicated:</p>

<blockquote><pre>
namespace std {
  template&lt;class charT, class traits = char_traits&lt;charT&gt;,
    class Allocator = allocator&lt;charT&gt; &gt;
  class basic_string {
  public:
    [&hellip;]
    basic_string&amp; append(const basic_string&amp; str, size_type pos, size_type n <ins>= npos</ins>);
    [&hellip;]
    basic_string&amp; assign(const basic_string&amp; str, size_type pos, size_type n <ins>= npos</ins>);
    [&hellip;]
    basic_string&amp; insert(size_type pos1, const basic_string&amp; str, size_type pos2, size_type n <ins>= npos</ins>);
    [&hellip;]
    basic_string&amp; replace(size_type pos1, size_type n1, const basic_string&amp; str, size_type pos2, size_type n2 <ins>= npos</ins>);
    [&hellip;]
    int compare(size_type pos1, size_type n1, const basic_string&amp; str, size_type pos2, size_type n2 <ins>= npos</ins>) const;
    [&hellip;]
  };
}
</pre></blockquote>
</li>

<li><p>Change 27.4.3.7.2 <a href="https://wg21.link/string.append">[string.append]</a> before p3 as indicated:</p>

<blockquote><pre>
basic_string&amp; append(const basic_string&amp; str, size_type pos, size_type n <ins>= npos</ins>);
</pre></blockquote>
</li>

<li><p>Change 27.4.3.7.3 <a href="https://wg21.link/string.assign">[string.assign]</a> before p4 as indicated:</p>

<blockquote><pre>
basic_string&amp; assign(const basic_string&amp; str, size_type pos, size_type n <ins>= npos</ins>);
</pre></blockquote>
</li>

<li><p>Change 27.4.3.7.4 <a href="https://wg21.link/string.insert">[string.insert]</a> before p5 as indicated:</p>

<blockquote><pre>
basic_string&amp; insert(size_type pos1, const basic_string&amp; str, size_type pos2, size_type n <ins>= npos</ins>);
</pre></blockquote>
</li>

<li><p>Change 27.4.3.7.6 <a href="https://wg21.link/string.replace">[string.replace]</a> before p5 as indicated:</p>

<blockquote><pre>
basic_string&amp; replace(size_type pos1, size_type n1, const basic_string&amp; str, size_type pos2, size_type n2 <ins>= npos</ins>);
</pre></blockquote>
</li>

<li><p>Change 27.4.3.8.4 <a href="https://wg21.link/string.compare">[string.compare]</a> before p4 as indicated:</p>

<blockquote><pre>
int compare(size_type pos1, size_type n1, const basic_string&amp; str, size_type pos2, size_type n2 <ins>= npos</ins>) const;
</pre></blockquote>
</li>
</ol>






</body>
</html>
