https://gcc.gnu.org/bugzilla/show_bug.cgi?id=106175

            Bug ID: 106175
           Summary: std::prev and next should check for equality with
                    std::begin
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: deco33000 at yandex dot com
  Target Milestone: ---

Hi,

To get better defined algorithms, we should not have a segfault in that case:

int main() 
{
    std::vector<int> v;

    // if (v.empty()) {return;}; <- should not need that line

    for (auto i = begin(v); i != prev(end(v)); i += 1){

        std::cout << *i << std::endl;

    } 

    return 0;

}

std::prev should always check for equality with begin() or empty() for its
argument.
If equal, do nothing to the iterator. The loop won't be entered.

Same for std::next, and by extension std::advance.

if we need to do specific actions in case the vector is empty, that is an
opt-in decision.

There may be some good counter arguments but I don't see them..

Reply via email to