C++26: Erroneous Behaviour
4 days ago
- #Safety
- #Programming
- #C++
- C++ conferences frequently discuss safety, highlighting the need for improvements in both safety and the language's reputation.
- Undefined behavior is a major issue affecting software safety and stability, with uninitialized variables being a common problem.
- C++26 introduces 'erroneous behavior' for uninitialized reads, replacing undefined behavior with a well-defined but incorrect behavior that compilers can diagnose.
- Compilers like clang and gcc already have techniques to identify uninitialized reads, but erroneous behavior makes it practical by ensuring diagnostics.
- Uninitialized objects will be initialized to an implementation-specific value under erroneous behavior, with compilers encouraged to diagnose such issues.
- The [[indeterminate]] attribute allows deliberate uninitialized variables, but using them without initialization results in undefined behavior.
- Erroneous behavior applies to automatic storage duration variables, but dynamic storage and pointers also pose uninitialized variable risks.
- Function parameters can also be marked with [[indeterminate]], affecting their behavior when uninitialized.
- As of January 2025, no compilers support erroneous behavior, but it represents a significant step forward in C++ safety.