[PATCH] libstdc++: Support constexpr formatting for vector<bool>::reference.
Tomasz KamiĆski <[email protected]> Thu, 6 Aug 2026 13:05:24 +0200
| Newsgroups | gmane.comp.gcc.patches,gmane.comp.gcc.libstdc++.devel |
|---|---|
| Message-ID | <[email protected]> |
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.
---
Noted this when I expanded constexpr queue/priority_queue to
make their formatters constexpr here:
https://gcc.gnu.org/pipermail/libstdc++/2026-August/067451.html
Testing on x86_64-linux. Modified test passed with all standard
modes. OK for trunk when all test passes?
libstdc++-v3/include/std/vector | 3 +++
.../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 86e42b22ea5..aeb7edce710 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 833727f4b41..856687c49f6 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();
}
--
2.55.0