[_GLIBCXX_DEBUG] Inline most basic checks
Jonathan Wakely
jwakely.gcc@gmail.com
Thu Apr 2 06:46:33 GMT 2026
Please ping for review after GCC 16 is released.
On Thu, 2 Apr 2026, 07:44 Jonathan Wakely, <jwakely.gcc@gmail.com> wrote:
> This needs to wait for GCC 17
>
> On Thu, 2 Apr 2026, 06:24 François Dumont, <frs.dumont@gmail.com> wrote:
>
>> As part of my patch to do some _GLIBCXX_DEBUG checks in constant
>> evaluation context:
>>
>> https://gcc.gnu.org/pipermail/libstdc++/2026-January/065132.html
>>
>> was also an occasion to optimize this mode for the most basic checks on
>> iterators that are singularity and comparability. Those can now be
>> inlined instead of going through a library call.
>>
>> In this patch I've isolated just those bits that won't have any impact
>> on constant evaluation. It will just call new _M_attach_to and
>> _M_detach_sequence that are doing nothing if in constant evaluation.
>>
>> As you'll see it also does some code simplifications and mark many
>> function as C++20 constexpr.
>>
>>
>> libstdc++: [_GLIBCXX_DEBUG] Make constant evaluation compatible
>>
>> Inline the most basic _GLIBCXX_DEBUG iterator checks _M_singular
>> and _M_can_compare.
>> In addition to being an optimization it also make those usable in a
>> constant evaluated
>> context.
>>
>> Provide also a C++20 constexpr _Safe_iterator_base _M_attach_to and
>> _M_detach_sequence
>> to call instead of respectively _M_attach and _M_detach that will
>> serve maybe later
>> to have some _GLIBCXX_DEBUG checks in constexpr expressions.
>>
>> Thanks to those changes make most of _GLIBCXX_DEBUG checks C++20
>> constexpr.
>>
>> libstdc++-v3/ChangeLog:
>>
>> * include/debug/forward_list (_Sequence_traits<>::_S_size):
>> Declare as C++20
>> constexpr.
>> * include/debug/functions.h
>> (__foreign_iterator): Declare as C++20 constexpr and add
>> std::__is_constant_evaluated
>> check.
>> * include/debug/helper_functions.h (__check_singular_aux):
>> Declare as C++20 constexpr.
>> * include/debug/list (_Sequence_traits<>::_S_size): Likewise.
>> * include/debug/safe_base.h
>> (_Safe_iterator_base::_M_attach_to): New, C++20 constexpr.
>> (_Safe_iterator_base(const _Safe_sequence_base*, bool):
>> Adapt to use latter without
>> __is_constant_evaluated check.
>> (_Safe_iterator_base(const _Safe_iterator_base&, bool)):
>> Likewise.
>> (_Safe_iterator_base::_M_detach_sequence): New, C++20
>> constexpr.
>> (~_Safe_iterator_base()): Adapt to use latter without
>> __is_constant_evaluated check.
>> (_Safe_iterator_base::_M_singular): Move private.
>> (_Safe_iterator_base::_M_can_compare): Move private.
>> (__check_singular_aux(const _Safe_iterator_base*)): Declare
>> as C++20 constexpr and
>> implement as such.
>> * include/debug/safe_iterator.h
>> (_BeforeBeginHelper<>::_S_Is): Declare as C++20 constexpr.
>> (_BeforeBeginHelper<>::_S_Is_Beginnest): Likewise.
>> (_Sequence_traits<>::_S_size): Likewise.
>> (_Safe_iterator::_Iter_base, _Safe_iterator::_Safe_base):
>> Remove.
>> (_Safe_iterator(const _Safe_iterator&, _Unchecked)):
>> Delegate to correct base classes
>> constructors.
>> (_Safe_iterator::_M_singular()): New, C++20 constexpr.
>> (_Safe_iterator::_M_can_compare(const
>> _Safe_iterator_base&)): New, C++20 constexpr.
>> (_Safe_iterator::_M_attach()): Declare as C++20 constexpr,
>> use base class
>> _M_attach_to().
>> (_Safe_iterator::_M_detach()): New, C++20 constexpr, use
>> base class _M_detach_sequence().
>> (_Safe_iterator::_M_dereferenceable): Declare as C++20
>> constexpr.
>> (_Safe_iterator::_M_before_dereferenceable): Likewise.
>> (_Safe_iterator::_M_incrementable): Likewise.
>> (_Safe_iterator::_M_value_initialized): Likewise.
>> (_Safe_iterator::_M_can_advance): Likewise.
>> (_Safe_iterator::_M_valid_range): Likewise.
>> (_Safe_iterator::_M_get_sequence): Likewise.
>> (_Safe_iterator::_M_get_distance_from_begin): Likewise.
>> (_Safe_iterator::_M_get_distance_to_end): Likewise.
>> (_Safe_iterator::_M_is_end): Likewise.
>> (_Safe_iterator::_M_is_before_begin): Likewise.
>> (_Safe_iterator::_M_is_beginnest): Likewise.
>> (_Safe_iterator::operator++(int)):
>> Declare C++20 constexpr and add __is_constant_evaluated to
>> skip debug check.
>> (_Safe_iterator::operator--(int)): Likewise.
>> (_Safe_iterator::_M_decrementable): Declare as C++20
>> constexpr.
>> (_Safe_iterator::_M_valid_range): Declare as C++20 constexpr.
>> (operator<(const _Safe_iterator&, const _Safe_iterator&)):
>> Likewise.
>> (operator<=(const _Safe_iterator&, const _Safe_iterator&)):
>> Likewise.
>> (operator>(const _Safe_iterator&, const _Safe_iterator&)):
>> Likewise.
>> (operator>=(const _Safe_iterator&, const _Safe_iterator&)):
>> Likewise.
>> (operator+(const _Safe_iterator&, difference_type)):
>> Likewise.
>> (operator+(difference_type, const _Safe_iterator&)):
>> Likewise.
>> (operator-(const _Safe_iterator&, difference_type)):
>> Likewise.
>> (operator-(difference_type, const _Safe_iterator&)):
>> Likewise.
>> * include/debug/safe_iterator.tcc: Adapt method with C++20
>> constexpr declaration.
>> * include/debug/safe_local_iterator.h
>> (_Safe_local_iterator::_Iter_base,
>> _Safe_local_iterator::_Safe_base): Remove.
>> (_Safe_local_iterator(const _Safe_local_iterator&,
>> _Unchecked)): Delegate to the correct
>> base classes constructors.
>> (_Safe_local_iterator::_M_singular): New.
>> (_Safe_local_iterator::_M_can_compare(const
>> _Safe_iterator_base&)): New.
>>
>> Tested under Linux x86_64 _GLIBCXX_DEBUG mode.
>>
>> Ok to commit ?
>>
>> François
>>
>>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://gcc.gnu.org/pipermail/libstdc++/attachments/20260402/8f5556c5/attachment-0001.htm>
More information about the Libstdc++
mailing list