From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2181) id 4AE52385840A; Fri, 14 Apr 2023 11:00:08 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4AE52385840A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1681470008; bh=7AhTKNnXlhXuo7A600aSVlnEe1KBSVnulvz+cDrMYLU=; h=From:To:Subject:Date:From; b=DBeWzEcjTzPQH77Yoaa8QFxWRJyIwtWxQ3We+aZ/u17lydmJ6yjaZllwJxOKzvei7 bT3RKUJM/Fcr0So5wE6iZDRcgAkKm6fk2bngSbW+XmuLqd3POWhB4IASSdznj6glu9 JgRmBvyrNaeM/dpxhqmqGlabwucob9MUQxMFE1Qc= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Jonathan Wakely To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc r13-7182] libstdc++: Improve diagnostics for invalid std::format calls X-Act-Checkin: gcc X-Git-Author: Jonathan Wakely X-Git-Refname: refs/heads/master X-Git-Oldrev: b0e85485fbf042abccee5c0a9eb499da386c8db3 X-Git-Newrev: 6a9547f3ca6c48f116478169d5fc62165dde7f43 Message-Id: <20230414110008.4AE52385840A@sourceware.org> Date: Fri, 14 Apr 2023 11:00:08 +0000 (GMT) List-Id: https://gcc.gnu.org/g:6a9547f3ca6c48f116478169d5fc62165dde7f43 commit r13-7182-g6a9547f3ca6c48f116478169d5fc62165dde7f43 Author: Jonathan Wakely Date: Thu Apr 13 16:34:51 2023 +0100 libstdc++: Improve diagnostics for invalid std::format calls 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> && ...), | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 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. Diff: --- libstdc++-v3/include/std/format | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/libstdc++-v3/include/std/format b/libstdc++-v3/include/std/format index 72b6b450ad1..e4ef4f9b6d9 100644 --- a/libstdc++-v3/include/std/format +++ b/libstdc++-v3/include/std/format @@ -137,7 +137,7 @@ namespace __format template 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 class _Checking_scanner : public _Scanner<_CharT> { + static_assert( + (is_default_constructible_v> && ...), + "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 + template 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(); }