public inbox for libstdc++@gcc.gnu.org
 help / color / mirror / Atom feed
* _LIBCXX_DEBUG value initialized singular iterators assert failures in std algorithms
@ 2024-03-11 20:07 Maciej Miera
  2024-03-11 20:40 ` Jonathan Wakely
  0 siblings, 1 reply; 19+ messages in thread
From: Maciej Miera @ 2024-03-11 20:07 UTC (permalink / raw)
  To: libstdc++

Hello,

I have tried to introduce an extra level of safety to my codebase and utilize _GLIBCXX_DEBUG in my test builds in order to catch faulty iterators.
However, I have encountered the following problem: I would like to utilize singular, value-initialized iterators as an arbitrary "null range”.
However, this leads to failed assertions in std:: algorithms taking such range.

Consider the following code sample with find_if:

#include <map>
#include <algorithm>
#include <iterator>

#ifndef __cpp_lib_null_iterators
#warning "Not standard compliant"
#endif

int main()
{
    std::multimap<char, int>::iterator it1{};
    std::multimap<char, int>::iterator it2{};

    (void) (it1==it2); // OK
    (void) std::find_if(
        it1, it2, [](const auto& el) { return el.second == 8;});
}

Compiled with -std=c++20 and -D_GLIBCXX_DEBUG it produces the warning "Not standard compliant"
and the execution results in the following assert failure:

/opt/compiler-explorer/gcc-12.2.0/include/c++/12.2.0/bits/stl_algo.h:3875:
In function:
    constexpr _IIter std::find_if(_IIter, _IIter, _Predicate) [with _IIter = 
    gnu_debug::_Safe_iterator<_Rb_tree_iterator<pair<const char, int> >, 
    debug::multimap<char, int>, bidirectional_iterator_tag>; _Predicate = 
    main()::<lambda(const auto:16&)>]

The question is though: is it by design, or is it just a mere oversight? The warning actually suggest the first option.
If it is an intentional design choice, could you provide some rationale behind it, please?

Compiler explorer link with demo below:
https://godbolt.org/z/e8xa5Eoqn

Best regards,
A M. Miera

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

end of thread, other threads:[~2024-03-21  6:20 UTC | newest]

Thread overview: 19+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-03-11 20:07 _LIBCXX_DEBUG value initialized singular iterators assert failures in std algorithms Maciej Miera
2024-03-11 20:40 ` Jonathan Wakely
2024-03-12  0:54   ` Maciej Miera
2024-03-12  1:03     ` Jonathan Wakely
2024-03-12  9:52       ` Jonathan Wakely
2024-03-14 21:49         ` _LIBCXX_DEBUG value initialized singular iterators assert failures in std algorithms [PR104316] François Dumont
2024-03-16 12:16           ` François Dumont
2024-03-17 11:45             ` Jonathan Wakely
2024-03-17 16:52               ` François Dumont
2024-03-17 18:14                 ` François Dumont
2024-03-18  7:45                   ` Jonathan Wakely
2024-03-18 21:38                     ` François Dumont
2024-03-19  9:31                       ` Jonathan Wakely
2024-03-19 15:41                         ` Jonathan Wakely
2024-03-20  5:59                           ` François Dumont
2024-03-20  9:02                             ` Jonathan Wakely
2024-03-20 18:10                               ` François Dumont
2024-03-21  6:20                                 ` Jonathan Wakely
2024-03-18  7:45                 ` Jonathan Wakely

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