]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
libstdc++: Improve diagnostics for invalid std::format calls
authorJonathan Wakely <jwakely@redhat.com>
Thu, 13 Apr 2023 15:34:51 +0000 (16:34 +0100)
committerJonathan Wakely <jwakely@redhat.com>
Fri, 14 Apr 2023 10:58:39 +0000 (11:58 +0100)
Add a static_assert and a comment so that calling std::format for
unformattable argument types will now show:

/home/jwakely/gcc/13/include/c++/13.0.1/format:3563:22: error: static assertion failed: std::formatter must be specialized for each format arg
 3563 |       static_assert((is_default_constructible_v<formatter<_Args, _CharT>> && ...),
      |                      ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

and:

  140 |       formatter() = delete; // No std::formatter specialization for this type.

libstdc++-v3/ChangeLog:

* include/std/format (formatter): Add comment to deleted default
constructor of primary template.
(_Checking_scanner): Add static_assert.

libstdc++-v3/include/std/format

index 72b6b450ad162f53d2304f61275bc4ee19e0a776..e4ef4f9b6d93535ebcdef6e6c3a2f852ee410d49 100644 (file)
@@ -137,7 +137,7 @@ namespace __format
   template<typename _Tp, typename _CharT = char>
     struct formatter
     {
-      formatter() = delete;
+      formatter() = delete; // No std::formatter specialization for this type.
       formatter(const formatter&) = delete;
       formatter& operator=(const formatter&) = delete;
     };
@@ -3560,6 +3560,10 @@ namespace __format
   template<typename _CharT, typename... _Args>
     class _Checking_scanner : public _Scanner<_CharT>
     {
+      static_assert(
+       (is_default_constructible_v<formatter<_Args, _CharT>> && ...),
+       "std::formatter must be specialized for each type being formatted");
+
     public:
       constexpr
       _Checking_scanner(basic_string_view<_CharT> __str)
@@ -3581,17 +3585,17 @@ namespace __format
        __builtin_unreachable();
       }
 
-      template<typename _Head, typename... _Tail>
+      template<typename _Tp, typename... _OtherArgs>
        constexpr void
        _M_parse_format_spec(size_t __id)
        {
          if (__id == 0)
            {
-             formatter<_Head, _CharT> __f;
+             formatter<_Tp, _CharT> __f;
              this->_M_pc.advance_to(__f.parse(this->_M_pc));
            }
-         else if constexpr (sizeof...(_Tail) != 0)
-           _M_parse_format_spec<_Tail...>(__id - 1);
+         else if constexpr (sizeof...(_OtherArgs) != 0)
+           _M_parse_format_spec<_OtherArgs...>(__id - 1);
          else
            __builtin_unreachable();
        }