Mark static_empty_bucket_ptr storage as const(expr) - #95
Open
jwnimmer-tri wants to merge 1 commit into
Open
Conversation
Even though the returned pointer must be non-const, it would be a mistake if any code in this library ever wrote into it. Marking the storage const (and casting away the const-ness on return) is a clear way to indicate that. (It may even show the fault during unit testing in case such a bug was ever introduced.) Furthermore, when C++20 is enabled we can mark it constexpr, which means that the storage is defined as part of the compiled library's readonly data and doesn't need the atomic-initialize-upon-first-use guard variable. This also helps to avoid ODR hazards when linking this header-only library into multiple shared libraries (as can happen with nanobind when in NB_STATIC mode). While we're here, we might as well mark all static constants constexpr as a cleanup -- even those not needed by the bucket_entry code.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Even though the returned pointer must be non-const, it would be a mistake if any code in this library ever wrote into it. Marking the storage const (and casting away the const-ness on return) is a clear way to indicate that. (It may even show the fault during unit testing in case such a bug was ever introduced.)
Furthermore, when C++20 is enabled we can mark it constexpr, which means that the storage is defined as part of the compiled library's readonly data and doesn't need the atomic-initialize-upon-first-use guard variable. This also helps to avoid ODR hazards when linking this header-only library into multiple shared libraries (as can happen with nanobind when in NB_STATIC mode).
While we're here, we might as well mark all static constants constexpr as a cleanup -- even those not needed by the bucket_entry code.