function_record code health enhancements (std::launder, std::is_standard_layout)
#5592
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
This PR refines and extends the use of
std::launderfor callable captures stored infunction_record::data. It generalizes the approach first introduced in PR #2816 by applying it more consistently and defensively.Key Improvements:
🔁 Consistent use of
std::launderAll reinterpretations of the
datafield into the internalcapturetype now go through a centralizedcapture::from_data(void**)helper, which appliesstd::launderwhere supported. This eliminates ad hoc casting and ensures correct behavior under the C++ object model.🛡️ Precondition enforcement
Adds a
static_assertonstd::is_standard_layout<capture>at critical optimization points. This ensures that reinterpret_casts and in-place storage remain well-defined.🧼 Macro centralization
Moves the
PYBIND11_STD_LAUNDERandPYBIND11_HAS_STD_LAUNDERmacro definitions tocommon.h, enabling their reuse across headers (e.g.,functional.hin addition topybind11.h).📉 Reduced reliance on compiler-specific warnings
The change removes special-case
#pragmause around-Wstrict-aliasingby ensuring that laundering is properly applied across the board.Motivation:
The original
std::launderusage in PR #2816 was narrow in scope and only applied during destruction of non-trivially destructible callables. This patch addresses potential undefined behavior more broadly and makes the low-level implementation safer and easier to reason about, while maintaining performance characteristics.ABI Note:
No changes to the ABI are introduced. The layout of
function_recordand the capture storage remains unchanged.Changelog: Not needed.
Upgrade guide: Not needed.
This PR is a by-product of work under PR #5585 (which we will not merge, most likely).
Browse function_record_std_launder/manuscript tree
Browse function_record_std_launder/manuscript commits