Title
Return value of std::regex_replace
Status
c++14
Section
[re.alg.replace]
Submitter
Pete Becker

Created on 2012-11-08.00:00:00 last changed 130 months ago

Messages

Date: 2014-02-13.06:35:03

Proposed resolution:

This wording is relative to N3485.

  1. Edit [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.

Date: 2014-02-13.06:35:03

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

Date: 2012-11-08.00:00:00

In [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?

History
Date User Action Args
2014-02-27 17:03:20adminsetstatus: wp -> c++14
2014-02-20 13:52:38adminsetstatus: immediate -> wp
2014-02-13 06:35:03adminsetmessages: + msg6833
2014-02-13 06:35:03adminsetstatus: new -> immediate
2012-11-14 20:44:53adminsetmessages: + msg6280
2012-11-08 00:00:00admincreate