From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2181) id 67173385801F; Fri, 12 Jan 2024 09:49:15 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 67173385801F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1705052956; bh=ENsrhaLwPsEutEHb+mfNgRgoinjj+N220QPkYTv6e2I=; h=From:To:Subject:Date:From; b=eSS78St/kAC6tNRC/1/W3/tFS0yWuME33Zi7MOKWtYxRsPTMGEIr7zq3c4AaHTvea 6itnHvQ/jSaRxeJrEBwzx1MM/BTvJDhz42UisTqVwcX22dxYyHg7SDFBZfQVhU3grz M4CDLVUxVIqw1qMeAg1WUIjotkbsLoAkaz8pv5JE= MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="utf-8" From: Jonathan Wakely To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc r14-7174] libstdc++: Fix std::runtime_format deviations from the spec [PR113320] X-Act-Checkin: gcc X-Git-Author: Jonathan Wakely X-Git-Refname: refs/heads/master X-Git-Oldrev: 35f8c661d67f53486f2f3e25887323476a7d0c2b X-Git-Newrev: 5fbc1b2e7c1bdf11f64765b278f477310c0f3436 Message-Id: <20240112094916.67173385801F@sourceware.org> Date: Fri, 12 Jan 2024 09:49:15 +0000 (GMT) List-Id: https://gcc.gnu.org/g:5fbc1b2e7c1bdf11f64765b278f477310c0f3436 commit r14-7174-g5fbc1b2e7c1bdf11f64765b278f477310c0f3436 Author: Jonathan Wakely Date: Wed Jan 10 20:54:11 2024 +0000 libstdc++: Fix std::runtime_format deviations from the spec [PR113320] I seem to have implemented this feature based on the P2918R0 revision, not the final P2918R2 one that was approved for C++26. This commit fixes it. The runtime-format-string type should not have a publicly accessible data member, so add a constructor and make it a friend of basic_format_string. It should also be non-copyable, so that it can only be constructed from a prvalue via temporary materialization. Change the basic_format_string constructor parameter to pass by value. Also add noexcept to the constructors and runtime_format generator functions. libstdc++-v3/ChangeLog: PR libstdc++/113320 * include/std/format (__format::_Runtime_format_string): Add constructor and disable copy operations. (basic_format_string(_Runtime_format_string)): Add noexcept and take parameter by value not rvalue reference. (runtime_format): Add noexcept. * testsuite/std/format/runtime_format.cc: Check noexcept. Check that construction is only possible from prvalues, not xvalues. Reviewed-by: Daniel Krügler Diff: --- libstdc++-v3/include/std/format | 30 ++++++++++++++++------ .../testsuite/std/format/runtime_format.cc | 11 ++++++++ 2 files changed, 33 insertions(+), 8 deletions(-) diff --git a/libstdc++-v3/include/std/format b/libstdc++-v3/include/std/format index 7440a25ea97..540f8b805f8 100644 --- a/libstdc++-v3/include/std/format +++ b/libstdc++-v3/include/std/format @@ -70,6 +70,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION // [format.context], class template basic_format_context template class basic_format_context; + // [format.fmt.string], class template basic_format_string + template struct basic_format_string; + /// @cond undocumented namespace __format { @@ -83,7 +86,20 @@ namespace __format using __format_context = basic_format_context<_Sink_iter<_CharT>, _CharT>; template - struct _Runtime_format_string { basic_string_view<_CharT> _M_str; }; + struct _Runtime_format_string + { + [[__gnu__::__always_inline__]] + _Runtime_format_string(basic_string_view<_CharT> __s) noexcept + : _M_str(__s) { } + + _Runtime_format_string(const _Runtime_format_string&) = delete; + void operator=(const _Runtime_format_string&) = delete; + + private: + basic_string_view<_CharT> _M_str; + + template friend struct std::basic_format_string; + }; } // namespace __format /// @endcond @@ -104,8 +120,6 @@ namespace __format template class basic_format_arg; - // [format.fmt.string], class template basic_format_string - /** A compile-time checked format string for the specified argument types. * * @since C++23 but available as an extension in C++20. @@ -119,7 +133,7 @@ namespace __format basic_format_string(const _Tp& __s); [[__gnu__::__always_inline__]] - basic_format_string(__format::_Runtime_format_string<_CharT>&& __s) + basic_format_string(__format::_Runtime_format_string<_CharT> __s) noexcept : _M_str(__s._M_str) { } @@ -144,14 +158,14 @@ namespace __format #if __cplusplus > 202302L [[__gnu__::__always_inline__]] inline __format::_Runtime_format_string - runtime_format(string_view __fmt) - { return {__fmt}; } + runtime_format(string_view __fmt) noexcept + { return __fmt; } #ifdef _GLIBCXX_USE_WCHAR_T [[__gnu__::__always_inline__]] inline __format::_Runtime_format_string - runtime_format(wstring_view __fmt) - { return {__fmt}; } + runtime_format(wstring_view __fmt) noexcept + { return __fmt; } #endif #endif // C++26 diff --git a/libstdc++-v3/testsuite/std/format/runtime_format.cc b/libstdc++-v3/testsuite/std/format/runtime_format.cc index 174334c7676..f2bfa5b434d 100644 --- a/libstdc++-v3/testsuite/std/format/runtime_format.cc +++ b/libstdc++-v3/testsuite/std/format/runtime_format.cc @@ -29,6 +29,17 @@ test_internal_api() VERIFY( s == "0x315" ); } +static_assert( noexcept(std::format_string<>(std::runtime_format(""))) ); +static_assert( noexcept(std::wformat_string<>(std::runtime_format(L""))) ); +static_assert( noexcept(std::format_string(std::runtime_format(""))) ); +static_assert( noexcept(std::wformat_string(std::runtime_format(L""))) ); +// A format string can be constructed from the result of std::runtime_format +// using copy elision, but cannot be constructed from an xvalue. +static_assert( !std::is_constructible_v, + decltype(std::runtime_format(""))&&> ); +static_assert( !std::is_constructible_v, + decltype(std::runtime_format(L""))&&> ); + int main() { test_char();