This page is a snapshot from the LWG issues list, see the Library Active Issues List for more information and the meaning of C++14 status.

2213. Return value of std::regex_replace

Section: 28.6.10.4 [re.alg.replace] Status: C++14 Submitter: Pete Becker Opened: 2012-11-08 Last modified: 2017-07-05

Priority: 0

View all other issues in [re.alg.replace].

View all issues with C++14 status.

Discussion:

In 28.6.10.4 [re.alg.replace], the first two variants of std::regex_replace 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. std::copy), 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?

[Issaquah 2014-02-11: Move to Immediate]

Proposed resolution:

This wording is relative to N3485.

  1. Edit 28.6.10.4 [re.alg.replace] as indicated:

    template <class OutputIterator, class BidirectionalIterator,
      class traits, class charT, class ST, class SA>
    OutputIterator
    regex_replace(OutputIterator out, BidirectionalIterator first, BidirectionalIterator last,
      const basic_regex<charT, traits>& e, const basic_string<charT, ST, SA>& fmt,
      regex_constants::match_flag_type flags = regex_constants::match_default);
    template <class OutputIterator, class BidirectionalIterator,
      class traits, class charT>
    OutputIterator
    regex_replace(OutputIterator out, BidirectionalIterator first, BidirectionalIterator last,
      const basic_regex<charT, traits>& e, const charT* fmt,
      regex_constants::match_flag_type flags = regex_constants::match_default);
    

    -1- Effects: Constructs a regex_iterator object i as if by regex_iterator<BidirectionalIterator, charT, traits> i(first, last, e, flags), and uses i to enumerate through all of the matches m of type match_results<BidirectionalIterator> that occur within the sequence [first, last). If no such matches are found and !(flags & regex_constants ::format_no_copy) then calls out = std::copy(first, last, out). If any matches are found then, for each such match, if !(flags & regex_constants::format_no_copy), calls out = std::copy(m.prefix().first, m.prefix().second, out), and then calls out = m.format(out, fmt, flags) for the first form of the function and out = m.format(out, fmt, fmt + char_traits<charT>::length(fmt), flags) for the second. Finally, if such a match is found and !(flags & regex_constants ::format_no_copy), calls out = std::copy(last_m.suffix().first, last_m.suffix().second, out) where last_m is a copy of the last match found. If flags & regex_constants::format_first_only is non-zero then only the first match found is replaced.

    -2- Returns: out.