Document number |
P3811R0 |
Date |
2025-08-15 |
Reply-to |
Jarrad J. Waterloo <descender76 at gmail dot com>
|
Audience |
SG23 Safety and Security |
default comparison memory safety
Table of contents
Abstract
The C++
standard should require compilers to implement defaulted comparison operator functions
free of memory safety related undefined behavior and this guarantee should be stated in the standard.
Is it unreasonable to require compilers to implement defaulted comparison operator functions
free of memory safety related undefined behavior? Consider the sample generated examples from the proposals that created the defaulted comparison operator functions
language feature.
Consistent comparison
<=> != ==
When do you actually use <=>?
How do these examples rank with respect to being free of memory safety related undefined behavior?
memory safety
|
🗸 |
uninitialized memory |
🗸 |
range access |
🗸 |
null pointer dereference |
🗸 |
dangling |
🗸 |
invalidation |
🗸 |
type safety |
Motivation
There has been sustained interest in creating islands of safety in C++
that goes beyond the constexpr
evaluation of core constant expressions
.
Safe C++
Strategy for removing safety-related UB by default
Most every day programmers are more specifically concerned about the memory unsafety subset of undefined behavior. While we can't at this moment mandate that all of C++
is free of memory unsafety, can we at least lock down the low hanging fruit that is already amenable to safety related verbiage.
Wording
11.10 Comparisons [class.compare]
11.10.1 Defaulted comparison operator functions [class.compare.default]
1 A defaulted comparison operator function (12.4.3) shall be a non-template function that
…
1.4 free of memory safety related undefined or erroneous behavior.
Impact on the standard
Safety not only requires safety to be in the language but also a part of the libraries even when those functions are created by the compiler. This proposal provides safety guarantees in some portions of the standard by requiring compiler implementers to make defaulted comparison operator functions
free of memory safety related undefined behavior.
The proposed changes are relative to the current working draft N5008
.
References
Jarrad J. Waterloo <descender76 at gmail dot com>
default comparison memory safety
Table of contents
Abstract
The
C++
standard should require compilers to implementdefaulted comparison operator functions
free of memory safety related undefined behavior and this guarantee should be stated in the standard.Is it unreasonable to require compilers to implement
defaulted comparison operator functions
free of memory safety related undefined behavior? Consider the sample generated examples from the proposals that created thedefaulted comparison operator functions
language feature.Consistent comparison
[1]<=> != ==
[2]When do you actually use <=>?
[3]How do these examples rank with respect to being free of memory safety related undefined behavior?
memory safety
Motivation
There has been sustained interest in creating islands of safety in
C++
that goes beyond theconstexpr
evaluation ofcore constant expressions
.Safe C++
[4]Strategy for removing safety-related UB by default
[5]Most every day programmers are more specifically concerned about the memory unsafety subset of undefined behavior. While we can't at this moment mandate that all of
C++
is free of memory unsafety, can we at least lock down the low hanging fruit that is already amenable to safety related verbiage.Wording
11.10 Comparisons [class.compare]
11.10.1 Defaulted comparison operator functions [class.compare.default]
1 A defaulted comparison operator function (12.4.3) shall be a non-template function that
…
1.4 free of memory safety related undefined or erroneous behavior.
Impact on the standard
Safety not only requires safety to be in the language but also a part of the libraries even when those functions are created by the compiler. This proposal provides safety guarantees in some portions of the standard by requiring compiler implementers to make
defaulted comparison operator functions
free of memory safety related undefined behavior.The proposed changes are relative to the current working draft
N5008
[6].References
https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/p0515r3.pdf ↩︎
https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p1185r2.html ↩︎
https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p1186r3.html ↩︎
https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2024/p3390r0.html ↩︎
https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2024/p3436r0.pdf ↩︎
https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2025/n5008.pdf ↩︎