[PATCH] libstdc++: Fix constification in range_formatter::format v2 [PR109162]

Tomasz Kamiński tkaminsk@redhat.com
Wed Apr 16 13:43:54 GMT 2025


Because the _M_format(__rg, __fc) were placed outside of if constexpr,
these method and it's childs where instantiated, even if _M_format<const _Range>
could be used. Now we put the calls in else branch of if constexpr.

libstdc++-v3/ChangeLog:

	* include/std/format (range_formatter::format): Do not instantiate
	_M_format for mutable _Rg if const _Rg can be used.
---
Testing on x86_64-linux, but there are no tests that will detect
uncessary instantiations.

 libstdc++-v3/include/std/format | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)

diff --git a/libstdc++-v3/include/std/format b/libstdc++-v3/include/std/format
index 27253f50ea8..d44c4b24abe 100644
--- a/libstdc++-v3/include/std/format
+++ b/libstdc++-v3/include/std/format
@@ -5636,12 +5636,15 @@ namespace __format
 	{
 	  using _Range = remove_reference_t<_Rg>;
 	  if constexpr (__format::__const_formattable_range<_Range, _CharT>)
-	  {
-	    using _CRef = ranges::range_reference_t<const _Range>;
-	    if constexpr (same_as<remove_cvref_t<_CRef>, _Tp>)
-	      return _M_format<const _Range>(__rg, __fc);
-	  }
-	  return _M_format(__rg, __fc);
+	    {
+	      using _CRef = ranges::range_reference_t<const _Range>;
+	      if constexpr (same_as<remove_cvref_t<_CRef>, _Tp>)
+		return _M_format<const _Range>(__rg, __fc);
+	      else
+		return _M_format(__rg, __fc);
+	    }
+	  else
+	    return _M_format(__rg, __fc);
 	}
 
     private:
-- 
2.49.0



More information about the Libstdc++ mailing list