A String Formatting Library in 65 Lines of C++
Posted4 months agoActive4 months ago
riki.houseTechstory
calmmixed
Debate
40/100
C++String FormattingLibrary Design
Key topics
C++
String Formatting
Library Design
A lightweight C++ string formatting library is presented, sparking discussion on its design choices, comparison to existing libraries like {fmt} and std::format, and potential improvements.
Snapshot generated from the HN discussion
Discussion Activity
Moderate engagementFirst comment
18m
Peak period
8
0-2h
Avg / period
2.6
Comment distribution23 data points
Loading chart...
Based on 23 loaded comments
Key moments
- 01Story posted
Sep 15, 2025 at 11:51 AM EDT
4 months ago
Step 01 - 02First comment
Sep 15, 2025 at 12:09 PM EDT
18m after posting
Step 02 - 03Peak activity
8 comments in 0-2h
Hottest window of the conversation
Step 03 - 04Latest activity
Sep 16, 2025 at 6:24 PM EDT
4 months ago
Step 04
Generating AI Summary...
Analyzing up to 500 comments to identify key contributors and discussion patterns
ID: 45251178Type: storyLast synced: 11/20/2025, 2:18:13 PM
Want the full context?
Jump to the original sources
Read the primary article or dive into the live Hacker News thread when you're ready.
In this day and age who knows when a dependency is hijacked :(
You do still need templates for the arguments (unless you're willing to resort to nasty preprocessor hackery, which would be needed if doing this in C - hmm, are the lifetime-of-temporary rules different too?), but it's pretty easy to just do:
where `to_owning_primitive` is the ADL'ed function you implement for every type you want to print, and `to_borrowed_primitive` probably only needs to be implemented for each string type (though I did find it also useful for wrapped integers of unknown size/rank, such as `time_t`).They do mention "std::print"[2] from C++23 (which uses std::format) and compile times, but, they don't touch on "std::format" at all.
See:
[1] https://en.cppreference.com/w/cpp/utility/format/format.html
[2] https://en.cppreference.com/w/cpp/io/print.html
Unfortunately, MSVC, always lags and fails to implement some things.
The kinds of places still waiting C++ aren’t usually the ones that put much emphasis on using a compiler from the past decade.
Java 8 and C++98 will be here forever lol
Comparing using `hyperfine --warmup 3 "g++ FILE.cpp"` (and some flags for fmt) I get 72ms vs 198ms. So I changed my mind and might take a crack at replacing {fmt} as well. Cool stuff! Thank you.
[1] https://vitaut.net/posts/2024/faster-cpp-compile-times/
[2] https://godbolt.org/z/3YaovhrjP bench-fmt.cpp
[3] https://godbolt.org/z/qMfM39P3q bench-rikifmt.cpp
I was curious about the "Why not printf" section, but I found code I don't understand there, too. For example this admittedly non-working snippet is cited as idiomatic:
Of corse this doesn't work (if the intent was to assemble the "hello world!" string, of which I'm not entirely sure), but not for the reason stated in TFA. You need to actually use cursor, not merely set it! :)One issue that I had is that printing floating-point values really needs the ability for the user to specify the precision and format. It's actually absurd that `std::to_string(double)` does not allow this.
Also, I believe `std::to_chars(double)` uses a fast algorithm and allows writing directly into a buffer.
$"i={i}"
For localization you might want numbered holes which makes it way more complicated.
You can detect if the backing buffer is too short, but can you detect other errors? Like having different numbers of holes and arguments? I couldn’t find any discussion about this.