public inbox for libstdc++-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r13-4060] libstc++: std::formattable concept should not be defined for C++20
@ 2022-11-15 14:29 Jonathan Wakely
  0 siblings, 0 replies; only message in thread
From: Jonathan Wakely @ 2022-11-15 14:29 UTC (permalink / raw)
  To: gcc-cvs, libstdc++-cvs

https://gcc.gnu.org/g:a5d4f38fbe3bf71efd465d5260955bd6675765fd

commit r13-4060-ga5d4f38fbe3bf71efd465d5260955bd6675765fd
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Tue Nov 15 13:59:51 2022 +0000

    libstc++: std::formattable concept should not be defined for C++20
    
    This concept was added by a C++23 proposal, so don't define it for
    C++20.
    
    Split the format/formatter/formatter.cc test into two parts, one that
    tests the C++20 requirements and one that tests the C++23 concept.
    
    libstdc++-v3/ChangeLog:
    
            * include/std/format (formattable): Only define for C++23/
            * testsuite/std/format/formatter.cc: Moved to...
            * testsuite/std/format/formatter/requirements.cc: ...here.
            * testsuite/std/format/formatter/concept.cc: New test.
            * testsuite/std/format/functions/format.cc: Replace use of
            std::formattable in C++20.

Diff:
---
 libstdc++-v3/include/std/format                    | 11 ++++-
 .../testsuite/std/format/formatter/concept.cc      | 46 ++++++++++++++++++
 .../{formatter.cc => formatter/requirements.cc}    | 54 +++++-----------------
 .../testsuite/std/format/functions/format.cc       | 12 ++++-
 4 files changed, 77 insertions(+), 46 deletions(-)

diff --git a/libstdc++-v3/include/std/format b/libstdc++-v3/include/std/format
index c79c8f2ce31..204a1710aca 100644
--- a/libstdc++-v3/include/std/format
+++ b/libstdc++-v3/include/std/format
@@ -2181,11 +2181,14 @@ namespace __format
 } // namespace __format
 /// @endcond
 
+#if __cplusplus > 202002L
   // [format.formattable], concept formattable
   template<typename _Tp, typename _CharT>
     concept formattable
       = __format::__formattable_impl<remove_reference_t<_Tp>, _CharT>;
+#endif
 
+#if __cpp_lib_format_ranges
   /// @cond undocumented
 namespace __format
 {
@@ -2199,6 +2202,7 @@ namespace __format
       = conditional_t<__const_formattable_range<_Rg, _CharT>, const _Rg, _Rg>;
 } // namespace __format
   /// @endcond
+#endif // format_ranges
 
   /// An iterator after the last character written, and the number of
   /// characters that would have been written.
@@ -3485,16 +3489,19 @@ namespace __format
 
 	std::visit_format_arg([this](auto& __arg) {
 	  using _Type = remove_reference_t<decltype(__arg)>;
+	  using _Formatter = typename _Context::template formatter_type<_Type>;
 	  if constexpr (is_same_v<_Type, monostate>)
 	    __format::__invalid_arg_id_in_format_string();
 	  else if constexpr (is_same_v<_Type, handle>)
 	    __arg.format(this->_M_pc, this->_M_fc);
-	  else
+	  else if constexpr (is_default_constructible_v<_Formatter>)
 	    {
-	      typename _Context::template formatter_type<_Type> __f;
+	      _Formatter __f;
 	      this->_M_pc.advance_to(__f.parse(this->_M_pc));
 	      this->_M_fc.advance_to(__f.format(__arg, this->_M_fc));
 	    }
+	  else
+	    static_assert(__format::__formattable_with<_Type, _Context>);
 	}, _M_fc.arg(__id));
       }
     };
diff --git a/libstdc++-v3/testsuite/std/format/formatter/concept.cc b/libstdc++-v3/testsuite/std/format/formatter/concept.cc
new file mode 100644
index 00000000000..fe56dc44a68
--- /dev/null
+++ b/libstdc++-v3/testsuite/std/format/formatter/concept.cc
@@ -0,0 +1,46 @@
+// { dg-options "-std=gnu++23" }
+// { dg-do compile { target c++23 } }
+
+#include <format>
+
+struct S { };
+
+template<> struct std::formatter<S> : std::formatter<const char*> {
+  template<class Out>
+  auto format(S, std::basic_format_context<Out, char>& ctx) const {
+    return formatter<const char*>::format("ess", ctx);
+  }
+};
+
+struct T { };
+
+template<> struct std::formatter<T> : std::formatter<const char*> {
+  // This function only accepts std::format_context, not other contexts.
+  auto format(T, std::format_context& ctx) const {
+    return formatter<const char*>::format("tee", ctx);
+  }
+};
+
+struct U { };
+
+void
+test_concept() // [format.formattable]
+{
+  static_assert( std::formattable<int, char> );
+  static_assert( std::formattable<const int, char> );
+  static_assert( std::formattable<int, wchar_t> );
+  static_assert( std::formattable<const int, wchar_t> );
+  static_assert( std::formattable<char, char> );
+  static_assert( std::formattable<char*, char> );
+  static_assert( std::formattable<wchar_t, wchar_t> );
+  static_assert( std::formattable<wchar_t*, wchar_t> );
+  static_assert( std::formattable<char, wchar_t> );
+  static_assert( ! std::formattable<char*, wchar_t> );
+  static_assert( ! std::formattable<wchar_t, char> );
+  static_assert( ! std::formattable<wchar_t*, char> );
+  static_assert( std::formattable<S, char> );
+  static_assert( std::formattable<const S, char> );
+  static_assert( ! std::formattable<S, wchar_t> ); // only formats as char
+  static_assert( ! std::formattable<T, char> ); // formatter not generic
+  static_assert( ! std::formattable<U, char> ); // no formatter
+}
diff --git a/libstdc++-v3/testsuite/std/format/formatter.cc b/libstdc++-v3/testsuite/std/format/formatter/requirements.cc
similarity index 50%
rename from libstdc++-v3/testsuite/std/format/formatter.cc
rename to libstdc++-v3/testsuite/std/format/formatter/requirements.cc
index 64ff2dbfbfd..3bff8bdbd5d 100644
--- a/libstdc++-v3/testsuite/std/format/formatter.cc
+++ b/libstdc++-v3/testsuite/std/format/formatter/requirements.cc
@@ -4,48 +4,6 @@
 #include <format>
 #include <testsuite_hooks.h>
 
-struct S { };
-
-template<> struct std::formatter<S> : std::formatter<const char*> {
-  template<class Out>
-  auto format(S, std::basic_format_context<Out, char>& ctx) const {
-    return formatter<const char*>::format("ess", ctx);
-  }
-};
-
-struct T { };
-
-template<> struct std::formatter<T> : std::formatter<const char*> {
-  // This function only accepts std::format_context, not other contexts.
-  auto format(T, std::format_context& ctx) const {
-    return formatter<const char*>::format("tee", ctx);
-  }
-};
-
-struct U { };
-
-void
-test_concept() // [format.formattable]
-{
-  static_assert( std::formattable<int, char> );
-  static_assert( std::formattable<const int, char> );
-  static_assert( std::formattable<int, wchar_t> );
-  static_assert( std::formattable<const int, wchar_t> );
-  static_assert( std::formattable<char, char> );
-  static_assert( std::formattable<char*, char> );
-  static_assert( std::formattable<wchar_t, wchar_t> );
-  static_assert( std::formattable<wchar_t*, wchar_t> );
-  static_assert( std::formattable<char, wchar_t> );
-  static_assert( ! std::formattable<char*, wchar_t> );
-  static_assert( ! std::formattable<wchar_t, char> );
-  static_assert( ! std::formattable<wchar_t*, char> );
-  static_assert( std::formattable<S, char> );
-  static_assert( std::formattable<const S, char> );
-  static_assert( ! std::formattable<S, wchar_t> ); // only formats as char
-  static_assert( ! std::formattable<T, char> ); // formatter not generic
-  static_assert( ! std::formattable<U, char> ); // no formatter
-}
-
 enum color { red, green, blue };
 const char* color_names[] = { "red", "green", "blue" };
 
@@ -62,6 +20,12 @@ test_specializations() // [format.formatter.spec]
 {
   std::string s0 = std::format("{}", 42); // OK, library-provided formatter
   VERIFY( s0 == "42" );
+  using Fi = std::format_context::formatter_type<int>;
+  static_assert( std::is_default_constructible_v<Fi> );
+  static_assert( std::is_copy_constructible_v<Fi> );
+  static_assert( std::is_move_constructible_v<Fi> );
+  static_assert( std::is_copy_assignable_v<Fi> );
+  static_assert( std::is_move_assignable_v<Fi> );
 
   // std::string s1 = std::format("{}", L"foo"); // error: disabled formatter
   using Fw = std::format_context::formatter_type<wchar_t>;
@@ -73,6 +37,12 @@ test_specializations() // [format.formatter.spec]
 
   std::string s2 = std::format("{}", red);  // OK, user-provided formatter
   VERIFY( s2 == "red" );
+  using Fc = std::format_context::formatter_type<color>;
+  static_assert( std::is_default_constructible_v<Fc> );
+  static_assert( std::is_copy_constructible_v<Fc> );
+  static_assert( std::is_move_constructible_v<Fc> );
+  static_assert( std::is_copy_assignable_v<Fc> );
+  static_assert( std::is_move_assignable_v<Fc> );
 
   // std::string s3 = std::format("{}", err{}); // error: disabled formatter
   using Ferr = std::format_context::formatter_type<err>;
diff --git a/libstdc++-v3/testsuite/std/format/functions/format.cc b/libstdc++-v3/testsuite/std/format/functions/format.cc
index 165ef41b4b3..c01405eac90 100644
--- a/libstdc++-v3/testsuite/std/format/functions/format.cc
+++ b/libstdc++-v3/testsuite/std/format/functions/format.cc
@@ -297,17 +297,25 @@ bool format_float()
     return s == "-0. != +0.500 ";
 }
 
+#if __cplusplus > 202002L
+template<typename T>
+concept formattable = std::formattable<T, char>;
+#else
+template<typename T>
+concept formattable = requires (T t, char* p) { std::to_chars(p, p, t); };
+#endif
+
 void
 test_float128()
 {
 #ifdef __SIZEOF_FLOAT128__
-  if constexpr (std::formattable<__float128, char>)
+  if constexpr (formattable<__float128>)
     VERIFY( format_float<__float128>() );
   else
     std::puts("Cannot format __float128 on this target");
 #endif
 #if __FLT128_DIG__
-  if constexpr (std::formattable<_Float128, char>)
+  if constexpr (formattable<_Float128>)
     VERIFY( format_float<_Float128>() );
   else
     std::puts("Cannot format _Float128 on this target");

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2022-11-15 14:29 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-11-15 14:29 [gcc r13-4060] libstc++: std::formattable concept should not be defined for C++20 Jonathan Wakely

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).