From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id AE299384DED6 for ; Mon, 8 Jan 2024 01:19:34 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org AE299384DED6 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org AE299384DED6 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1704676777; cv=none; b=kFMENc1IrjFphGJV2F3pwJRgSVZCgHc+FhaKeRer7pq9Jm1ri1fF4Nr+XwFMYV6qHmuAT6b8hoHKy0cMNNtvDs1+HQ6jdh2/GbxR9TLhe+S4LWtUAqlFC6tKMjYvpntipNOK1+KdfdZBXXM1QG7pgMY9PGqAa5E1BDPgKAsbj/8= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1704676777; c=relaxed/simple; bh=8CLkMpn1ClKZ5gR5r53C8c2rFX9D0Q6LrzwZvBLsFuo=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=m9KUo+dakc+noRDKrcv9DtGruZVqsvigj1DknBJl+IgBHeTShacFMmUU27ml9qVRrzI62sno0vhw/xHGc8OqUvg2Zwm9KV+9OayAP+BHetiDiGu6rEadjkR+FOkeAoJKDLktVPUWnQxKnB5RoERpWszS3WDYxNFOdqfDctvOX+Q= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1704676774; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=KGHIXQYPGMXDxTv/Uybr2vkMZxLCnPQa5K5JszZ76/k=; b=di671runRc9K+5kMVg4poJ5/rlrrMa+w9K8kPsl/RLfh8Kgtw9jp80c9IgYPYN6L2UtU8s qcXi9WX7QajVn6TahOei4O4SyDYEH/YLWXWdyWJHqzl93mqeUljxXpW4Z/G9S+gIpKnGms XXJMPLTiMR3JRyKDM85KRVkNS6TatbQ= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-322-d0LZP_I3MNO2DUyseYe2yw-1; Sun, 07 Jan 2024 20:19:32 -0500 X-MC-Unique: d0LZP_I3MNO2DUyseYe2yw-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 9EB66862DE2; Mon, 8 Jan 2024 01:19:32 +0000 (UTC) Received: from localhost (unknown [10.42.28.185]) by smtp.corp.redhat.com (Postfix) with ESMTP id 692201C060AF; Mon, 8 Jan 2024 01:19:32 +0000 (UTC) From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed 2/2] libstdc++: Implement P2918R0 "Runtime format strings II" for C++26 Date: Mon, 8 Jan 2024 01:19:02 +0000 Message-ID: <20240108011930.3670651-2-jwakely@redhat.com> In-Reply-To: <20240108011930.3670651-1-jwakely@redhat.com> References: <20240108011930.3670651-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.7 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-12.6 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,KAM_NUMSUBJECT,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE,TXREP,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: Tested x86_64-linux and aarch64-linux. Pushed to trunk. -- >8 -- This adds std::runtime_format for C++26. These new overloaded functions enhance the std::format API so that it isn't necessary to use the less ergonomic std::vformat and std::make_format_args (which are meant to be implementation details). This was approved in Kona 2023 for C++26. libstdc++-v3/ChangeLog: * include/std/format (__format::_Runtime_format_string): Define new class template. (basic_format_string): Add non-consteval constructor for runtime format strings. (runtime_format): Define new function for C++26. * testsuite/std/format/runtime_format.cc: New test. --- libstdc++-v3/include/std/format | 22 +++++++++++ .../testsuite/std/format/runtime_format.cc | 37 +++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 libstdc++-v3/testsuite/std/format/runtime_format.cc diff --git a/libstdc++-v3/include/std/format b/libstdc++-v3/include/std/format index 160efa5155c..b3b5a0bbdbc 100644 --- a/libstdc++-v3/include/std/format +++ b/libstdc++-v3/include/std/format @@ -81,6 +81,9 @@ namespace __format template using __format_context = basic_format_context<_Sink_iter<_CharT>, _CharT>; + + template + struct _Runtime_format_string { basic_string_view<_CharT> _M_str; }; } // namespace __format /// @endcond @@ -115,6 +118,11 @@ namespace __format consteval basic_format_string(const _Tp& __s); + [[__gnu__::__always_inline__]] + basic_format_string(__format::_Runtime_format_string<_CharT>&& __s) + : _M_str(__s._M_str) + { } + [[__gnu__::__always_inline__]] constexpr basic_string_view<_CharT> get() const noexcept @@ -133,6 +141,20 @@ namespace __format = basic_format_string...>; #endif +#if __cplusplus > 202302L + [[__gnu__::__always_inline__]] + inline __format::_Runtime_format_string + runtime_format(string_view __fmt) + { return {__fmt}; } + +#ifdef _GLIBCXX_USE_WCHAR_T + [[__gnu__::__always_inline__]] + inline __format::_Runtime_format_string + runtime_format(wstring_view __fmt) + { return {__fmt}; } +#endif +#endif // C++26 + // [format.formatter], formatter /// The primary template of std::formatter is disabled. diff --git a/libstdc++-v3/testsuite/std/format/runtime_format.cc b/libstdc++-v3/testsuite/std/format/runtime_format.cc new file mode 100644 index 00000000000..174334c7676 --- /dev/null +++ b/libstdc++-v3/testsuite/std/format/runtime_format.cc @@ -0,0 +1,37 @@ +// { dg-do run { target c++26 } } + +#include +#include + +void +test_char() +{ + std::string fmt = "{}"; + auto s = std::format(std::runtime_format(fmt), 123); + VERIFY( s == "123" ); +} + +void +test_wchar() +{ + std::wstring fmt = L"{:#o}"; + auto s = std::format(std::runtime_format(fmt), 456); + VERIFY( s == L"0710" ); +} + +void +test_internal_api() +{ + // Using _Runtime_format_string directly works even in C++20 mode. + // This can be used internally by libstdc++. + std::string fmt = "{:#x}"; + auto s = std::format(std::__format::_Runtime_format_string(fmt), 789); + VERIFY( s == "0x315" ); +} + +int main() +{ + test_char(); + test_wchar(); + test_internal_api(); +} -- 2.43.0