<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 726: Missing regex_replace() overloads</title>
<meta property="og:title" content="Issue 726: Missing regex_replace() overloads">
<meta property="og:description" content="C++ library issue. Status: NAD">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue726.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#NAD">NAD</a> status.</em></p>
<h3 id="726"><a href="lwg-closed.html#726">726</a>. Missing <code>regex_replace()</code> overloads</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#NAD">NAD</a>
 <b>Submitter:</b> Stephan T. Lavavej <b>Opened:</b> 2007-09-22 <b>Last modified:</b> 2016-01-28</p>
<p><b>Priority: </b>Not Prioritized
</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#NAD">NAD</a> status.</p>
<p><b>Discussion:</b></p>
<p>
Two overloads of <code>regex_replace()</code> are currently provided:
</p>

<blockquote><pre>
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 basic_string&lt;charT&gt;&amp; fmt, 
                regex_constants::match_flag_type flags = 
                  regex_constants::match_default);
 
template &lt;class traits, class charT&gt; 
  basic_string&lt;charT&gt; 
  regex_replace(const basic_string&lt;charT&gt;&amp; s, 
                const basic_regex&lt;charT, traits&gt;&amp; e, 
                const basic_string&lt;charT&gt;&amp; fmt, 
                regex_constants::match_flag_type flags = 
                  regex_constants::match_default);
</pre></blockquote>

<ol>
<li>Overloads taking <code>const charT *</code> are provided for <code>regex_match()</code> and
<code>regex_search()</code>, but not <code>regex_replace()</code>.  This is inconsistent.</li>
<li>
<p>The absence of <code>const charT *</code> overloads prevents ordinary-looking code from compiling, such as:</p>

<blockquote><pre>
const string s("kitten");
const regex r("en");
cout &lt;&lt; regex_replace(s, r, "y") &lt;&lt; endl;
</pre></blockquote>

<p>
The compiler error message will be something like "could not deduce
template argument for 'const std::basic_string&lt;_Elem&gt; &amp;' from 'const
char[1]'".
</p>

<p>
Users expect that anything taking a <code>basic_string&lt;charT&gt;</code> can also take a
<code>const charT *</code>.  In their own code, when they write a function taking
<code>std::string</code> (or <code>std::wstring</code>), they can pass a <code>const char *</code> (or <code>const
wchar_t *</code>), thanks to <code>basic_string</code>'s implicit constructor.  Because the
regex algorithms are templated on <code>charT</code>, they can't rely on
<code>basic_string</code>'s implicit constructor (as the compiler error message
indicates, template argument deduction fails first).
</p>

<p>
If a user figures out what the compiler error message means, workarounds
are available - but they are all verbose.  Explicit template arguments
could be given to <code>regex_replace()</code>, allowing <code>basic_string</code>'s implicit
constructor to be invoked - but <code>charT</code> is the last template argument, not
the first, so this would be extremely verbose.  Therefore, constructing
a <code>basic_string</code> from each C string is the simplest workaround.
</p>
</li>

<li>
There is an efficiency consideration: constructing <code>basic_string</code>s can
impose performance costs that could be avoided by a library
implementation taking C strings and dealing with them directly. 
(Currently, for replacement sources, C strings can be converted into
iterator pairs at the cost of verbosity, but for format strings, there
is no way to avoid constructing a <code>basic_string</code>.)
</li>
</ol>

<p><i>[
Sophia Antipolis:
]</i></p>


<blockquote><p>
We note that Boost already has these overloads. However, the proposed
wording is provided only for 28.6.10.4 <a href="https://wg21.link/re.alg.replace">[re.alg.replace]</a>; wording is needed for the synopsis
as well. We also note that this has impact on <code>match_results::format</code>,
which may require further overloads.
</p></blockquote>

<p><i>[
2009-07 Frankfurt:
]</i></p>


<blockquote><p>
Daniel to tweak for us.
</p></blockquote>

<p><i>[
2009-07-25 Daniel tweaks both this issue and <a href="lwg-defects.html#727" title="regex_replace() doesn't accept basic_strings with custom traits and allocators (Status: C++11)">727</a><sup><a href="https://cplusplus.github.io/LWG/issue727" title="Latest snapshot">(i)</a></sup>.
]</i></p>


<blockquote>
<p>
This is solved by the proposed resolution of <a href="lwg-defects.html#727" title="regex_replace() doesn't accept basic_strings with custom traits and allocators (Status: C++11)">727</a><sup><a href="https://cplusplus.github.io/LWG/issue727" title="Latest snapshot">(i)</a></sup>.
</p>
</blockquote>

<p><i>[
2009-10 Santa Cruz:
]</i></p>


<blockquote><p>
Leave Open. Though we believe this is solved by the proposed resolution
to <a href="lwg-defects.html#727" title="regex_replace() doesn't accept basic_strings with custom traits and allocators (Status: C++11)">727</a><sup><a href="https://cplusplus.github.io/LWG/issue727" title="Latest snapshot">(i)</a></sup>.
</p></blockquote>

<p><i>[
2010-01-27 Moved to Tentatively NAD after 5 positive votes on c++std-lib. 
Rationale added below.
]</i></p>



<p><b>Rationale:</b></p>
<p>
Solved by <a href="lwg-defects.html#727" title="regex_replace() doesn't accept basic_strings with custom traits and allocators (Status: C++11)">727</a><sup><a href="https://cplusplus.github.io/LWG/issue727" title="Latest snapshot">(i)</a></sup>.
</p>


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

<p>
Provide additional overloads for <code>regex_replace()</code>: one additional
overload of the iterator-based form (taking <code>const charT* fmt</code>), and three
additional overloads of the convenience form (one taking <code>const charT*
str</code>, another taking <code>const charT* fmt</code>, and the third taking both <code>const
charT* str</code> and <code>const charT* fmt</code>).  28.6.10.4 <a href="https://wg21.link/re.alg.replace">[re.alg.replace]</a>:
</p>

<blockquote>
<pre>
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 basic_string&lt;charT&gt;&amp; fmt, 
                regex_constants::match_flag_type flags = 
                  regex_constants::match_default);

<ins>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);</ins>
</pre>
<p>...</p>
<pre>
template &lt;class traits, class charT&gt; 
  basic_string&lt;charT&gt; 
  regex_replace(const basic_string&lt;charT&gt;&amp; s, 
                const basic_regex&lt;charT, traits&gt;&amp; e, 
                const basic_string&lt;charT&gt;&amp; fmt, 
                regex_constants::match_flag_type flags = 
                  regex_constants::match_default);

<ins>template &lt;class traits, class charT&gt; 
  basic_string&lt;charT&gt; 
  regex_replace(const basic_string&lt;charT&gt;&amp; s, 
                const basic_regex&lt;charT, traits&gt;&amp; e, 
                const charT* fmt, 
                regex_constants::match_flag_type flags = 
                  regex_constants::match_default);</ins>

<ins>template &lt;class traits, class charT&gt; 
  basic_string&lt;charT&gt; 
  regex_replace(const charT* s, 
                const basic_regex&lt;charT, traits&gt;&amp; e, 
                const basic_string&lt;charT&gt;&amp; fmt, 
                regex_constants::match_flag_type flags = 
                  regex_constants::match_default);</ins>

<ins>template &lt;class traits, class charT&gt; 
  basic_string&lt;charT&gt; 
  regex_replace(const charT* s, 
                const basic_regex&lt;charT, traits&gt;&amp; e, 
                const charT* fmt, 
                regex_constants::match_flag_type flags = 
                  regex_constants::match_default);</ins>
</pre>
</blockquote>






</body>
</html>
