[committed,2/2] libstdc++: Implement P2918R0 "Runtime format strings II" for C++26
Checks
Commit Message
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
Comments
Am Mo., 8. Jan. 2024 um 03:25 Uhr schrieb Jonathan Wakely <jwakely@redhat.com>:
>
> 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<typename _CharT>
> using __format_context = basic_format_context<_Sink_iter<_CharT>, _CharT>;
> +
> + template<typename _CharT>
> + 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)
> + { }
> +
My understanding is that this constructor should be noexcept according to N4971.
> [[__gnu__::__always_inline__]]
> constexpr basic_string_view<_CharT>
> get() const noexcept
> @@ -133,6 +141,20 @@ namespace __format
> = basic_format_string<wchar_t, type_identity_t<_Args>...>;
> #endif
>
> +#if __cplusplus > 202302L
> + [[__gnu__::__always_inline__]]
> + inline __format::_Runtime_format_string<char>
> + runtime_format(string_view __fmt)
> + { return {__fmt}; }
> +
> +#ifdef _GLIBCXX_USE_WCHAR_T
> + [[__gnu__::__always_inline__]]
> + inline __format::_Runtime_format_string<wchar_t>
> + runtime_format(wstring_view __fmt)
> + { return {__fmt}; }
> +#endif
> +#endif // C++26
> +
These runtime_format overloads should also be noexcept.
- Daniel
On Wed, 10 Jan 2024 at 18:33, Daniel Krügler <daniel.kruegler@gmail.com> wrote:
>
> Am Mo., 8. Jan. 2024 um 03:25 Uhr schrieb Jonathan Wakely <jwakely@redhat.com>:
> >
> > 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<typename _CharT>
> > using __format_context = basic_format_context<_Sink_iter<_CharT>, _CharT>;
> > +
> > + template<typename _CharT>
> > + 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)
> > + { }
> > +
>
> My understanding is that this constructor should be noexcept according to N4971.
>
> > [[__gnu__::__always_inline__]]
> > constexpr basic_string_view<_CharT>
> > get() const noexcept
> > @@ -133,6 +141,20 @@ namespace __format
> > = basic_format_string<wchar_t, type_identity_t<_Args>...>;
> > #endif
> >
> > +#if __cplusplus > 202302L
> > + [[__gnu__::__always_inline__]]
> > + inline __format::_Runtime_format_string<char>
> > + runtime_format(string_view __fmt)
> > + { return {__fmt}; }
> > +
> > +#ifdef _GLIBCXX_USE_WCHAR_T
> > + [[__gnu__::__always_inline__]]
> > + inline __format::_Runtime_format_string<wchar_t>
> > + runtime_format(wstring_view __fmt)
> > + { return {__fmt}; }
> > +#endif
> > +#endif // C++26
> > +
>
> These runtime_format overloads should also be noexcept.
Good catch. Looks like I implemented it from the P2918R0 proposal, not
the final R2. Oops.
I'll apply the changes, thanks!
@@ -81,6 +81,9 @@ namespace __format
template<typename _CharT>
using __format_context = basic_format_context<_Sink_iter<_CharT>, _CharT>;
+
+ template<typename _CharT>
+ 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<wchar_t, type_identity_t<_Args>...>;
#endif
+#if __cplusplus > 202302L
+ [[__gnu__::__always_inline__]]
+ inline __format::_Runtime_format_string<char>
+ runtime_format(string_view __fmt)
+ { return {__fmt}; }
+
+#ifdef _GLIBCXX_USE_WCHAR_T
+ [[__gnu__::__always_inline__]]
+ inline __format::_Runtime_format_string<wchar_t>
+ runtime_format(wstring_view __fmt)
+ { return {__fmt}; }
+#endif
+#endif // C++26
+
// [format.formatter], formatter
/// The primary template of std::formatter is disabled.
new file mode 100644
@@ -0,0 +1,37 @@
+// { dg-do run { target c++26 } }
+
+#include <format>
+#include <testsuite_hooks.h>
+
+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<char>(fmt), 789);
+ VERIFY( s == "0x315" );
+}
+
+int main()
+{
+ test_char();
+ test_wchar();
+ test_internal_api();
+}