<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 2213: Return value of std::regex_replace</title>
<meta property="og:title" content="Issue 2213: Return value of std::regex_replace">
<meta property="og:description" content="C++ library issue. Status: C++14">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue2213.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="2213"><a href="lwg-defects.html#2213">2213</a>. Return value of <code>std::regex_replace</code></h3>
<p><b>Section:</b> 28.6.10.4 <a href="https://wg21.link/re.alg.replace">[re.alg.replace]</a> <b>Status:</b> <a href="lwg-active.html#C++14">C++14</a>
 <b>Submitter:</b> Pete Becker <b>Opened:</b> 2012-11-08 <b>Last modified:</b> 2017-07-05</p>
<p><b>Priority: </b>0
</p>
<p><b>View all other</b> <a href="lwg-index.html#re.alg.replace">issues</a> in [re.alg.replace].</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>
In 28.6.10.4 <a href="https://wg21.link/re.alg.replace">[re.alg.replace]</a>, the first two variants of <code>std::regex_replace</code> take an output iterator named 
"out" as their first argument. Paragraph 2 of that section says that the functions return "out". When I first implemented 
this, many years ago, I wrote it to return the value of the output iterator after all the insertions (cf. <code>std::copy</code>), 
which seems like the most useful behavior. But looking at the requirement now, it like the functions should return the 
original value of "out" (i.e. they have to keep a copy of the iterator for no reason except to return it). Is that 
really what was intended?
</p>

<p><i>[Issaquah 2014-02-11: Move to Immediate]</i></p>




<p id="res-2213"><b>Proposed resolution:</b></p>
<p>This wording is relative to N3485.</p>

<ol>
<li><p>Edit 28.6.10.4 <a href="https://wg21.link/re.alg.replace">[re.alg.replace]</a> as indicated:</p>
<blockquote><pre>
template &lt;class OutputIterator, class BidirectionalIterator,
  class traits, class charT, class ST, class SA&gt;
OutputIterator
regex_replace(OutputIterator out, BidirectionalIterator first, BidirectionalIterator last,
  const basic_regex&lt;charT, traits&gt;&amp; e, const basic_string&lt;charT, ST, SA&gt;&amp; fmt,
  regex_constants::match_flag_type flags = regex_constants::match_default);
template &lt;class OutputIterator, class BidirectionalIterator,
  class traits, class charT&gt;
OutputIterator
regex_replace(OutputIterator out, BidirectionalIterator first, BidirectionalIterator last,
  const basic_regex&lt;charT, traits&gt;&amp; e, const charT* fmt,
  regex_constants::match_flag_type flags = regex_constants::match_default);
</pre><blockquote>
<p>
-1- <i>Effects</i>: Constructs a <code>regex_iterator</code> object <code>i</code> as if by 
<code>regex_iterator&lt;BidirectionalIterator, charT, traits&gt; i(first, last, e, flags)</code>, 
and uses <code>i</code> to enumerate through all of the matches <code>m</code> of type 
<code>match_results&lt;BidirectionalIterator&gt;</code> that occur within the sequence <code>[first, last)</code>. 
If no such matches are found and <code>!(flags &amp; regex_constants ::format_no_copy)</code> then calls
<code><ins>out = </ins>std::copy(first, last, out)</code>. If any matches are found then, for each such match, if 
<code>!(flags &amp; regex_constants::format_no_copy)</code>, calls <code><ins>out = </ins>std::copy(m.prefix().first, 
m.prefix().second, out)</code>, and then calls <code><ins>out = </ins>m.format(out, fmt, flags)</code> 
for the first form of the function and <code><ins>out = </ins>m.format(out, fmt, fmt + 
char_traits&lt;charT&gt;::length(fmt), flags)</code> for the second. Finally, if such a match is found 
and <code>!(flags &amp; regex_constants ::format_no_copy)</code>, calls 
<code><ins>out = </ins>std::copy(last_m.suffix().first, last_m.suffix().second, out)</code> where 
<code>last_m</code> is a copy of the last match found. If <code>flags &amp; regex_constants::format_first_only</code> 
is non-zero then only the first match found is replaced.
<p/>
-2- <i>Returns</i>: <code>out</code>.
</p>
</blockquote></blockquote>
</li>
</ol>






</body>
</html>
