[libcxx-commits] [libcxx] [libc++] Refactor std::print to allow for constant folding of the format part (PR #185459)

Louis Dionne via libcxx-commits libcxx-commits at lists.llvm.org
Fri Mar 13 07:02:40 PDT 2026


================
@@ -0,0 +1,27 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17
+
+#include <print>
+#include <string>
+
+#include "benchmark/benchmark.h"
+#include "make_string.h"
+#include "test_macros.h"
+
+template <class CharT>
+static void BM_string_without_formatting(benchmark::State& state) {
+  FILE* null = fopen("/dev/null", "w");
+  for (auto _ : state) {
+    std::print(null, "Hello, World!");
+  }
+}
+BENCHMARK(BM_string_without_formatting<char>)->Name("std::print(\"Hello, World!\")");
----------------
ldionne wrote:

While we're at it, let's also add a benchmark where the string is not constant-folded. I would do that by using a string that the compiler can't see (e.g. clobbered?) as the input to `print`.

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


More information about the libcxx-commits mailing list