]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
libstc++: std::formattable concept should not be defined for C++20
authorJonathan Wakely <jwakely@redhat.com>
Tue, 15 Nov 2022 13:59:51 +0000 (13:59 +0000)
committerJonathan Wakely <jwakely@redhat.com>
Tue, 15 Nov 2022 14:01:39 +0000 (14:01 +0000)
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.

libstdc++-v3/include/std/format
libstdc++-v3/testsuite/std/format/formatter/concept.cc [new file with mode: 0644]
libstdc++-v3/testsuite/std/format/formatter/requirements.cc [moved from libstdc++-v3/testsuite/std/format/formatter.cc with 50% similarity]
libstdc++-v3/testsuite/std/format/functions/format.cc

index c79c8f2ce3160245615c16bae9e4ec9415efd24a..204a1710aca800f1ca287332e9b7753eb0dc6c81 100644 (file)
@@ -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 (file)
index 0000000..fe56dc4
--- /dev/null
@@ -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
+}
similarity index 50%
rename from libstdc++-v3/testsuite/std/format/formatter.cc
rename to libstdc++-v3/testsuite/std/format/formatter/requirements.cc
index 64ff2dbfbfd2724105bca34759d1a67c00d45af2..3bff8bdbd5d65e9d1a712896f0bae90a9398aa10 100644 (file)
@@ -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>;
index 165ef41b4b345f4503843c4fce2522955cbc7013..c01405eac900e87b6748a3dec8948995c77d37ea 100644 (file)
@@ -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");