[libcxx-commits] [libcxx] [libc++] Add basic constant folding for std::format (PR #107197)

Mark de Wever via libcxx-commits libcxx-commits at lists.llvm.org
Sun Feb 2 10:50:40 PST 2025


================
@@ -447,10 +449,41 @@ format_to(_OutIt __out_it, wformat_string<_Args...> __fmt, _Args&&... __args) {
 }
 #  endif
 
+template <class _CharT>
+[[nodiscard]] _LIBCPP_HIDE_FROM_ABI optional<basic_string<_CharT>> __try_constant_folding_format(
+    basic_string_view<_CharT> __fmt,
+    basic_format_args<basic_format_context<back_insert_iterator<__format::__output_buffer<_CharT>>, _CharT>> __args) {
+  // [[gnu::pure]] is added to ensure that the compiler always knows that this call can be eliminated.
+  if (bool __is_identity =
+          [&] [[__gnu__::__pure__]] { return std::ranges::find_first_of(__fmt, array{'{', '}'}) == __fmt.end(); }();
----------------
mordante wrote:

In format we typically do the latter, so it reads very odd to do things differently here. I'm even a bit surprised this compiles since you want to compare `wchar_t` against `char`.

https://github.com/llvm/llvm-project/pull/107197


More information about the libcxx-commits mailing list