[gcc r17-3329] libstdc++: Support constexpr formatting for vector<bool>::reference.

Tomasz Kaminski via Gcc-cvs <[email protected]>
Newsgroups gmane.comp.gcc.cvs
Message-ID <20260817104522.78AE64BA2E1C__37812.3811142117$1786963532$gmane$org@sourceware.org>
https://gcc.gnu.org/g:07337728c028420ec122f95b60ef449259ddccc9

commit r17-3329-g07337728c028420ec122f95b60ef449259ddccc9
Author: Tomasz Kamiński <[email protected]>
Date:   Thu Aug 6 12:52:10 2026 +0200

    libstdc++: Support constexpr formatting for vector<bool>::reference.
    
    This specialization was made constexpr as part of P3391R2, "constexpr
    format", and was missed in r17-1162-g42f9bf2e3c56ae.
    
    libstdc++-v3/ChangeLog:
    
            * include/std/vector (formatter<_Bit_reference, _CharT>::format)
            [__glibcxx_constexpr_format]: Declare as constexpr.
            * testsuite/23_containers/vector/bool/format.cc
            [__glibcxx_constexpr_format]: Run test suite at compile
            time.
    
    Reviewed-by: Jonathan Wakely <[email protected]>
    Signed-off-by: Tomasz Kamiński <[email protected]>

Diff:
---
 libstdc++-v3/include/std/vector                    |  3 +++
 .../testsuite/23_containers/vector/bool/format.cc  | 26 ++++++++++++++++++----
 2 files changed, 25 insertions(+), 4 deletions(-)

diff --git a/libstdc++-v3/include/std/vector b/libstdc++-v3/include/std/vector
index 86e42b22ea5b..aeb7edce7100 100644
--- a/libstdc++-v3/include/std/vector
+++ b/libstdc++-v3/include/std/vector
@@ -148,6 +148,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       // Standard declares this as template accepting unconstrained
       // FormatContext type.
       template<typename _Out>
+#ifdef __glibcxx_constexpr_format // C++ >= 26 && HOSTED && CXX11 string
+	constexpr
+#endif
 	typename basic_format_context<_Out, _CharT>::iterator
 	format(const _GLIBCXX_STD_C::_Bit_reference& __u,
 	       basic_format_context<_Out, _CharT>& __fc) const
diff --git a/libstdc++-v3/testsuite/23_containers/vector/bool/format.cc b/libstdc++-v3/testsuite/23_containers/vector/bool/format.cc
index 833727f4b418..856687c49f6f 100644
--- a/libstdc++-v3/testsuite/23_containers/vector/bool/format.cc
+++ b/libstdc++-v3/testsuite/23_containers/vector/bool/format.cc
@@ -5,12 +5,18 @@
 #include <vector>
 #include <testsuite_hooks.h>
 
+#ifdef __glibcxx_constexpr_format
+# define CONSTEXPR constexpr
+#else
+# define CONSTEXPR
+#endif
+
 static_assert(!std::formattable<std::vector<bool>::reference, int>);
 static_assert(!std::formattable<std::vector<bool>::reference, char32_t>);
 static_assert(std::enable_nonlocking_formatter_optimization<std::vector<bool>::reference>);
 
 template<typename... Args>
-bool
+CONSTEXPR bool
 is_format_string_for(const char* str, Args&&... args)
 {
   try {
@@ -24,7 +30,7 @@ is_format_string_for(const char* str, Args&&... args)
 #define WIDEN_(C, S) ::std::__format::_Widen<C>(S, L##S)
 #define WIDEN(S) WIDEN_(CharT, S)
 
-void
+CONSTEXPR void
 test_format_string()
 {
   std::vector<bool> v(1, true);
@@ -36,7 +42,7 @@ test_format_string()
 }
 
 template<typename CharT>
-void
+CONSTEXPR void
 test_output()
 {
   std::basic_string<CharT> res;
@@ -65,9 +71,21 @@ test_output()
   VERIFY( res == WIDEN("[1, 0]") );
 }
 
-int main()
+CONSTEXPR bool
+test_all()
 {
   test_format_string();
   test_output<char>();
   test_output<wchar_t>();
+ 
+  return true;
+}
+
+#ifdef __glibcxx_constexpr_format
+static_assert(test_all());
+#endif
+
+int main()
+{
+  test_all();
 }
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.