Home
Ban std::string
2025-06-22 Sun

Don’t use std::string in C++

The use of std::string should be banned in C++ code bases.
I’m sure this statement sounds like heresy and you want to burn me at stake. But is it really controversial?
Java, C#, Go, JavaScript, Python, Ruby, PHP: they all have immutable strings that are basically 2 machine words: a pointer to string data and size of the string.
If they have an equivalent of std:string it’s something like StringBuilder.
C++ should also use immutable strings in 97% of situations.
The problem is gravity: the existing code, the culture. They all pull you strongly towards std::string and going against the current is the hardest thing there is.
There isn’t a standard type for that. You can use newish std::span<char*> but there really should be std::str (or some such).
I did ban std::string in SumatraPDF where I mostly pass char* but I don’t expect many other C++ code bases to switch away from std::string.

activity

More Edna porting. I’m on a roll. Typically I tried to work in small chunks so that it’s easier to track down a change that broke something but here I made a few big changes, mostly out of the necessity. If the change I’m porting is big and touches lots of places, it’s hard to port it in smaller chunks. Will take some testing to iron things out
daily thoughts
Jun 22 2025

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you: