[gcc r17-440] libstdc++: Reorder compile-time checks for __formatter_str::_M_format_range.

Tomasz Kaminski tkaminsk@gcc.gnu.org
Mon May 11 09:35:32 GMT 2026


https://gcc.gnu.org/g:0646376b3e340ef1a0a55d143e38410f75f81276

commit r17-440-g0646376b3e340ef1a0a55d143e38410f75f81276
Author: Tomasz Kamiński <tkaminsk@redhat.com>
Date:   Wed May 6 15:22:06 2026 +0200

    libstdc++: Reorder compile-time checks for __formatter_str::_M_format_range.
    
    If _M_format_range was called with prvalue of span S (or any contiguous_range),
    the previous chain of if-contexpr will call _M_format_range<S&>, then
    _M_format_range<const S&> and then format(string_view). By checking for
    contiguous_range first, it calls format(string_view) direclty, removing
    unnecessary instantiations and symbols.
    
    Similary, for all prvalues of type R, that meet __simply_formattable_range R,
    we were instantiating _M_format_range<R&> and then _M_format_range<const R&>.
    By moving the if for __simply_formattable_range before is_lvalue_reference_v,
    we call _M_format_range<const R&> direclty.
    
    libstdc++-v3/ChangeLog:
    
            * include/std/format (__formatter_str::_M_format_range):
            Reorder constexpr checks, to reduce number of instantiations.
    
    Reviewed-by: Jonathan Wakely <jwakely@redhat.com>
    Signed-off-by: Tomasz Kamiński <tkaminsk@redhat.com>

Diff:
---
 libstdc++-v3/include/std/format | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/libstdc++-v3/include/std/format b/libstdc++-v3/include/std/format
index 60adca93b5d8..0f20fcc8d883 100644
--- a/libstdc++-v3/include/std/format
+++ b/libstdc++-v3/include/std/format
@@ -1438,17 +1438,17 @@ namespace __format
 	{
 	  using _Range = remove_reference_t<_Rg>;
 	  using _String_view = basic_string_view<_CharT>;
-	  if constexpr (!is_lvalue_reference_v<_Rg>)
-	    return _M_format_range<_Range&>(__rg, __fc);
-	  else if constexpr (!is_const_v<_Range>
-			       && __simply_formattable_range<_Range, _CharT>)
-	    return _M_format_range<const _Range&>(__rg, __fc);
-	  else if constexpr (ranges::contiguous_range<_Rg>)
+	  if constexpr (ranges::contiguous_range<_Rg>)
 	    {
 	      _String_view __str(ranges::data(__rg),
 				 size_t(ranges::distance(__rg)));
 	      return format(__str, __fc);
 	    }
+	  else if constexpr (!is_const_v<_Range>
+			        && __simply_formattable_range<_Range, _CharT>)
+	    return _M_format_range<const _Range&>(__rg, __fc);
+	  else if constexpr (!is_lvalue_reference_v<_Rg>)
+	    return _M_format_range<_Range&>(__rg, __fc);
 	  else
 	    {
 	      auto __handle_debug = [this, &__rg]<typename _NOut>(_NOut __nout)


More information about the Libstdc++-cvs mailing list