public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug libstdc++/106175] New: std::prev and next should check for equality with std::begin
@ 2022-07-03 15:10 deco33000 at yandex dot com
  2022-07-03 17:37 ` [Bug libstdc++/106175] " redi at gcc dot gnu.org
                   ` (7 more replies)
  0 siblings, 8 replies; 9+ messages in thread
From: deco33000 at yandex dot com @ 2022-07-03 15:10 UTC (permalink / raw)
  To: gcc-bugs

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..

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2022-07-04  6:28 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-07-03 15:10 [Bug libstdc++/106175] New: std::prev and next should check for equality with std::begin deco33000 at yandex dot com
2022-07-03 17:37 ` [Bug libstdc++/106175] " redi at gcc dot gnu.org
2022-07-03 17:54 ` deco33000 at yandex dot com
2022-07-03 18:00 ` deco33000 at yandex dot com
2022-07-03 18:00 ` deco33000 at yandex dot com
2022-07-03 19:04 ` pinskia at gcc dot gnu.org
2022-07-03 19:09 ` deco33000 at yandex dot com
2022-07-03 20:46 ` pinskia at gcc dot gnu.org
2022-07-04  6:28 ` deco33000 at yandex dot com

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).