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

3204. sub_match::swap only swaps the base class

Section: 30.9 [re.submatch] Status: New Submitter: Jonathan Wakely Opened: 2019-05-07 Last modified: 2019-06-13

Priority: 3

View other active issues in [re.submatch].

View all other issues in [re.submatch].

View all issues with New status.

Discussion:

sub_match<I> derives publicly from pair<I,I>, and so inherits pair::swap(pair&). This means that the following program fails:

#include <regex>
#include <cassert>

int main()
{
  std::sub_match<const char*> a, b;
  a.matched = true;
  a.swap(b);
  assert(b.matched);
}

The pair::swap(pair&) member should be hidden by a sub_match::swap(sub_match&) member that does the right thing.

[2019-06-12 Priority set to 3 after reflector discussion]

Proposed resolution:

This wording is relative to N4810.

  1. Modify 30.9 [re.submatch], class template sub_match synopsis, as indicated:

    template<class BidirectionalIterator>
    class sub_match : public pair<BidirectionalIterator, BidirectionalIterator> {
    public:
      […]
      int compare(const sub_match& s) const;
      int compare(const string_type& s) const;
      int compare(const value_type* s) const;
      
      void swap(sub_match& s) noexcept(see below);
    };
    
  2. Modify 30.9.1 [re.submatch.members] as indicated:

    int compare(const value_type* s) const;
    

    […]

    void swap(sub_match& s) noexcept(see below);
    
    [Drafting note: The swappable requirement should really be unnecessary because Cpp17Iterator requires it, but there is no wording that requires BidirectionalIterator to satisfy the bidirectional iterator requirements. — end drafting note]

    -?- Expects: Lvalues of type BidirectionalIterator are swappable (16.5.3.2 [swappable.requirements]).

    -?- Effects: Equivalent to:

    this->pair<BidirectionalIterator, BidirectionalIterator>::swap(s);
    std::swap(matched, s.matched);
    

    -?- Remarks: The expression inside noexcept is equivalent to is_nothrow_swappable_v<BidirectionalIterator>.