C++26 includes standard library hardening (P3471). Albeit Bitcoin Core uses C++20 as of https://github.com/bitcoin/bitcoin/blob/51166559808c3528f148b5c0c38cb4481e536dd8/CMakeLists.txt#L74, the feature can be enabled in earlier versions of C++, according to https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2025/p3471r4.html#enabling-hardening. So the question is whether it should be done.
Some benefits would be:
- Less unsafe code, when preconditions are enforced by the standard library. This can help to find bugs, such as #32122.
- Less manual precondition checking, which is verbose and sometimes wrong. For example, see #32255 (comment).
The downsides would be:
- The performance impact need to be checked. According to https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2025/p3471r4.html#deployment-experience, the impact was minimal at google in production when using libc++, except for some hot paths. However, the same may not be true for us, or for other standard libraries. For example, see a libstdc++ result here: https://www.github.com/bitcoin/bitcoin/pull/31424#discussion_r1910658664.
- There is no portable way to enable it for all standard libraries, so our build system would have to deal with it.
For now this is a brainstorming issue, to gather any benefits, downsides or questions that I’ve missed.