]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
libstdc++: Reorder compile-time checks for __formatter_str::_M_format_range.
authorTomasz Kamiński <tkaminsk@redhat.com>
Wed, 6 May 2026 13:22:06 +0000 (15:22 +0200)
committerTomasz Kamiński <tkaminsk@redhat.com>
Mon, 11 May 2026 09:31:42 +0000 (11:31 +0200)
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>
libstdc++-v3/include/std/format

index 60adca93b5d801d5d1708fd3d32370038517d82c..0f20fcc8d883c9483edb4a73b8f60f44e9679e95 100644 (file)
@@ -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)