Title
Have `hive::erase_if` reevaluate `end()` to avoid UB
Status
ready
Section
[hive.erasure]
Submitter
Frank Birbacher

Created on 2025-08-16.00:00:00 last changed yesterday

Messages

Date: 2025-08-29.11:30:22

Proposed resolution:

This wording is relative to N5014.

[Drafting note: There are other ways to fix this code while keeping the caching behaviour, but I don't see any particular reason to do so for the definition of the effects.]

  1. Modify [hive.erasure] as indicated:

    template<class T, class Allocator, class Predicate>
      typename hive<T, Allocator>::size_type
        erase_if(hive<T, Allocator>& c, Predicate pred);
    

    -2- Effects: Equivalent to:

    auto original_size = c.size();
    for (auto i = c.begin(), last = c.end(); i != lastc.end(); ) {
      if (pred(*i)) {
        i = c.erase(i);
      } else {
        ++i;
      }
    }
    return original_size - c.size();
    
Date: 2025-08-15.00:00:00

[ 2025-08-29; Reflector poll ]

Set status to Tentatively Ready after five votes in favour during reflector poll.

Date: 2025-08-16.00:00:00

Background: https://github.com/cplusplus/draft/pull/8162

For [hive.erasure] p2, the defining code must not cache the end-iterator. In case the last element of the sequence is removed, the past-the-end iterator will be invalidated. This will trigger UB in the loop condition. Instead, re-evaluate `end()` each time.

History
Date User Action Args
2025-08-29 11:30:22adminsetmessages: + msg14974
2025-08-29 11:30:22adminsetstatus: new -> ready
2025-08-17 09:30:59adminsetmessages: + msg14939
2025-08-16 00:00:00admincreate