<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 727: regex_replace() doesn't accept basic_strings with custom traits and allocators</title>
<meta property="og:title" content="Issue 727: regex_replace() doesn't accept basic_strings with custom traits and allocators">
<meta property="og:description" content="C++ library issue. Status: C++11">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue727.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++11">C++11</a> status.</em></p>
<h3 id="727"><a href="lwg-defects.html#727">727</a>. <code>regex_replace()</code> doesn't accept <code>basic_string</code>s with custom traits and allocators</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++11">C++11</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#C++11">C++11</a> status.</p>
<p><b>Discussion:</b></p>
<p>
<code>regex_match()</code> and <code>regex_search()</code> take <code>const basic_string&lt;charT, ST,
SA&gt;&amp;</code>.  <code>regex_replace()</code> takes <code>const basic_string&lt;charT&gt;&amp;</code>.  This prevents
<code>regex_replace()</code> from accepting <code>basic_string</code>s with custom traits and
allocators.
</p>

<p>
Overloads of <code>regex_replace()</code> taking <code>basic_string</code> should be additionally
templated on <code>class ST, class SA</code> and take <code>const basic_string&lt;charT, ST,
SA&gt;&amp;</code>.  Consistency with <code>regex_match()</code> and <code>regex_search()</code> would place
<code>class ST, class SA</code> as the first template arguments; compatibility with
existing code using TR1 and giving explicit template arguments to
<code>regex_replace()</code> would place <code>class ST, class SA</code> as the last template
arguments.
</p>

<p><i>[
Batavia (2009-05):
]</i></p>

<blockquote>
<p>
Bill comments, "We need to look at the depth of this change."
</p>
<p>
Pete remarks that we are here dealing with a convenience function
that saves a user from calling the iterato-based overload.
</p>
<p>
Move to Open.
</p>
</blockquote>

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


<blockquote><p>
Howard to ask Stephan Lavavej to provide wording.
</p></blockquote>

<p><i>[
2009-07-17 Stephan provided wording.
]</i></p>


<p><i>[
2009-07-25 Daniel tweaks both this issue and <a href="lwg-closed.html#726" title="Missing regex_replace() overloads (Status: NAD)">726</a><sup><a href="https://cplusplus.github.io/LWG/issue726" title="Latest snapshot">(i)</a></sup>.
]</i></p>


<blockquote>
<p>
One relevant part of the proposed resolution below suggests
to add a new overload of the format member function in the
<code>match_results</code> class template that accepts two character pointers
defining the <code>begin</code> and <code>end</code> of a format range. A more general
approach could have proposed a pair of iterators instead, but
the used pair of char pointers reflects existing practice. If the
committee strongly favors an iterator-based signature, this
could be simply changed. I think that the minimum requirement
should be a <code>BidirectionalIterator</code>, but current implementations
take advantage (at least partially) of the <code>RandomAccessIterator</code>
sub interface of the char pointers.
</p>

<p><b>Suggested Resolution:</b></p>

<p><i>[Moved into the proposed resloution]</i></p>



</blockquote>

<p><i>[
2009-07-30 Stephan agrees with Daniel's wording.  Howard places Daniel's wording
in the Proposed Resolution.
]</i></p>


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


<blockquote><p>
Move to Review. Chair is anxious to move this to Ready in Pittsburgh.
</p></blockquote>

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




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

<ol>
<li>
<p>
Change 28.6.3 <a href="https://wg21.link/re.syn">[re.syn]</a> as indicated:
</p>

<blockquote><pre>
// 28.11.4, function template regex_replace:
template &lt;class OutputIterator, class BidirectionalIterator,
          class traits, class charT<ins>, class ST, class SA</ins>&gt;
  OutputIterator
  regex_replace(OutputIterator out,
                BidirectionalIterator first, BidirectionalIterator last,
                const basic_regex&lt;charT, traits&gt;&amp; e,
                const basic_string&lt;charT<ins>, ST, SA</ins>&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>

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

<ins>
template &lt;class traits, class charT, class ST, class SA&gt;
  basic_string&lt;charT, ST, SA&gt;
  regex_replace(const basic_string&lt;charT, ST, SA&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, class ST, class SA&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, ST, SA&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>
</li>

<li>
<p>
Change 28.6.9 <a href="https://wg21.link/re.results">[re.results]</a>&#47;3, class template <code>match_results</code> as
indicated:
</p>

<blockquote><pre>
<ins>
template &lt;class OutputIter&gt;
  OutputIter
  format(OutputIter out,
         const char_type* fmt_first, const char_type* fmt_last,
         regex_constants::match_flag_type flags =
           regex_constants::format_default) const;
</ins>

template &lt;class OutputIter<ins>, class ST, class SA</ins>&gt;
  OutputIter
  format(OutputIter out,
         const <del>string_type</del><ins>basic_string&lt;char_type, ST, SA&gt;</ins>&amp; fmt,
         regex_constants::match_flag_type flags =
           regex_constants::format_default) const;

<ins>template &lt;class ST, class SA&gt;</ins>
  <del>string_type</del><ins>basic_string&lt;char_type, ST, SA&gt;</ins>
  format(const <del>string_type</del><ins>basic_string&lt;char_type, ST, SA&gt;</ins>&amp; fmt,
         regex_constants::match_flag_type flags =
           regex_constants::format_default) const;

<ins>
string_type
format(const char_type* fmt,
       regex_constants::match_flag_type flags =
         regex_constants::format_default) const;
</ins>
</pre></blockquote>
</li>

<li>
<p>
Insert at the very beginning of 28.6.9.6 <a href="https://wg21.link/re.results.form">[re.results.form]</a> the following:
</p>

<blockquote><pre>
<ins>
template &lt;class OutputIter&gt;
  OutputIter
  format(OutputIter out,
         const char_type* fmt_first, const char_type* fmt_last,
         regex_constants::match_flag_type flags =
           regex_constants::format_default) const;
</ins>
</pre>
<blockquote>

<p><ins>
1 <i>Requires:</i> The type <code>OutputIter</code> shall satisfy the requirements for an
Output Iterator (24.3.5.4 <a href="https://wg21.link/output.iterators">[output.iterators]</a>).
</ins></p>

<p><ins>
2 <i>Effects:</i> Copies the character sequence <code>[fmt_first,fmt_last)</code> to
<code>OutputIter out</code>. Replaces each format specifier or escape sequence in
the copied range with either the character(s) it represents or the sequence of
characters within <code>*this</code> to which it refers.  The bitmasks specified in
<code>flags</code> determine which format specifiers and escape sequences are
recognized.
</ins></p>

<p><ins>
3 <i>Returns:</i> <code>out</code>.
</ins></p>
</blockquote>
</blockquote>
</li>

<li>
<p>
Change 28.6.9.6 <a href="https://wg21.link/re.results.form">[re.results.form]</a>, before p. 1 until p. 3 as indicated:
</p>

<blockquote><pre>
template &lt;class OutputIter<ins>, class ST, class SA</ins>&gt;
  OutputIter
  format(OutputIter out,
         const <del>string_type</del><ins>basic_string&lt;char_type, ST, SA&gt;</ins>&amp; fmt,
         regex_constants::match_flag_type flags =
           regex_constants::format_default) const;
</pre>

<blockquote>
<p>
<del>1 <i>Requires:</i> The type <code>OutputIter</code> shall satisfy the requirements for
an Output Iterator (24.2.3).</del>
</p>

<p>
2 <i>Effects:</i> <del>Copies the character sequence
<code>[fmt.begin(),fmt.end())</code> to <code>OutputIter out</code>. Replaces each
format specifier or escape sequence in <code>fmt</code> with either the character(s)
it represents or the sequence of characters within <code>*this</code> to which it
refers. The bitmasks specified in <code>flags</code> determines what format
specifiers and escape sequences are recognized</del> <ins>Equivalent to
<code>return format(out, fmt.data(), fmt.data() + fmt.size(), flags)</code></ins>.
</p>

<p>
<del>3 <i>Returns:</i> <code>out</code>.</del>
</p>
</blockquote>
</blockquote>
</li>

<li>
<p>
Change 28.6.9.6 <a href="https://wg21.link/re.results.form">[re.results.form]</a>, before p. 4 until p. 4 as indicated:
</p>

<blockquote><pre>
<ins>template &lt;class ST, class SA&gt;</ins>
  <del>string_type</del><ins>basic_string&lt;char_type, ST, SA&gt;</ins>
  format(const <del>string_type</del><ins>basic_string&lt;char_type, ST, SA&gt;</ins>&amp; fmt,
         regex_constants::match_flag_type flags =
           regex_constants::format_default) const;
</pre>

<blockquote>
<p>
<i>Effects:</i> <del>Returns a copy of the string <code>fmt</code>. Replaces each format
specifier or escape sequence
in <code>fmt</code> with either the character(s) it represents or the sequence of
characters within <code>*this</code> to which
it refers. The bitmasks specified in flags determines what format
specifiers and escape sequences are
recognized.</del> <ins>Constructs an empty string <code>result</code> of type
<code>basic_string&lt;char_type, ST, SA&gt;</code>,
and calls <code>format(back_inserter(result), fmt, flags)</code>.</ins>
</p>

<p>
<ins><i>Returns:</i> <code>result</code></ins>
</p>
</blockquote>
</blockquote>
</li>

<li>
<p>
At the end of 28.6.9.6 <a href="https://wg21.link/re.results.form">[re.results.form]</a> insert as indicated:
</p>

<blockquote><pre>
<ins>
string_type
  format(const char_type* fmt,
         regex_constants::match_flag_type flags =
           regex_constants::format_default) const;
</ins></pre>

<blockquote>
<p>
<ins><i>Effects:</i> Constructs an empty string <code>result</code> of type <code>string_type</code>, and calls
<code>format(back_inserter(result), fmt, fmt +
char_traits&lt;char_type&gt;::length(fmt), flags)</code>.</ins>
</p>
<p>
<ins><i>Returns:</i> <code>result</code></ins>
</p>
</blockquote>
</blockquote>

</li>

<li>
<p>
Change 28.6.10.4 <a href="https://wg21.link/re.alg.replace">[re.alg.replace]</a> before p. 1 as indicated:
</p>

<blockquote><pre>
template &lt;class OutputIterator, class BidirectionalIterator,
          class traits, class charT<ins>, class ST, class SA</ins>&gt;
  OutputIterator
  regex_replace(OutputIterator out,
                BidirectionalIterator first, BidirectionalIterator last,
                const basic_regex&lt;charT, traits&gt;&amp; e,
                const basic_string&lt;charT<ins>, ST, SA</ins>&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>

<blockquote><p>
<i>Effects:</i> [..]. If any matches are found then, for each such match, if <code>!(flags &amp;
 regex_constants::format_no_copy)</code> calls <code>std::copy(m.prefix().first,
m.prefix().second,
 out)</code>, and then calls <code>m.format(out, fmt, flags)</code> <ins>for the first
form of the function
 and <code>m.format(out, fmt, fmt + char_traits&lt;charT&gt;::length(fmt), flags)</code>
for the second
 form</ins>. [..].
</p></blockquote>
</blockquote>
</li>

<li>
<p>
Change 28.6.10.4 <a href="https://wg21.link/re.alg.replace">[re.alg.replace]</a> before p. 3 as indicated:
</p>

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

<ins>
template &lt;class traits, class charT, class ST, class SA&gt;
  basic_string&lt;charT, ST, SA&gt;
  regex_replace(const basic_string&lt;charT, ST, SA&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></pre>

<blockquote><p>
<i>Effects:</i> Constructs an empty string <code>result</code> of type <code>basic_string&lt;charT<ins>,
ST, SA</ins>&gt;</code>, calls <code>regex_replace(back_inserter(result), s.begin(), s.end(),
e, fmt, flags)</code>, and then returns <code>result</code>.
</p></blockquote>
</blockquote>
</li>

<li>
<p>
At the end of 28.6.10.4 <a href="https://wg21.link/re.alg.replace">[re.alg.replace]</a> add the following new prototype description:
</p>

<blockquote><pre>
<ins>
template &lt;class traits, class charT, class ST, class SA&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, ST, SA&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><p>
<ins>
<i>Effects:</i> Constructs an empty string <code>result</code> of type <code>basic_string&lt;charT&gt;</code>,
calls <code>regex_replace(back_inserter(result), s, s +
char_traits&lt;charT&gt;::length(s),
e, fmt, flags)</code>, and then returns <code>result</code>.
</ins>
</p></blockquote>
</blockquote>
</li>

</ol>







</body>
</html>
