Conversation
Conversion between pointers to unrelated types `Source` and `Target`
can be done with two `static_cast`'s
("upcast" to pointer to cv-void followed by "downcast")
=> (IMO) it should be done with `static_cast`
(in order not to give the impression that `reinterpret_cast` really is needed):
`T *ptr_target = static_cast <T *> (static_cast <void *> (ptr_source));`
(Maybe that was the original intent of introducing `pdata`.)
…bject. If the compiler does not optimize away the `pdata` function-local `static` object, it ends up occupying space in the data section and it is going to need a corresponding relocation entry (just in case the module is not loaded at the desired address, as is the case with ASLR in Windows Vista and later and in modern versions of Linux). I admit that, for the current code, both MSVC and GCC manage to optimize away `pdata` even if it is `static`. But Boost should be an example of good coding which can be applied even to more complicated source code, e.g. to source code which calls `opaque_function (&pdata)` (in which case, if `pdata` is static, both MSVC and GCC do emit an extra relocation). And for other more complex cases, a function-local static-storage-duration object costs even more (as compared to a function-local automatic-storage-duration object given C++11 rules for "thread-safe" initialization of function-local `static`s. And I wish to re-iterate that, in my opinion, a good way to cast a pointer-to-one-type to a pointer-to-an-unrelated-type is not by `reinterpret_cast` (which by the way makes `pdata` not needed any more), but by using pointer to (possibly cv-qualified) `void` as an intermediary pointer type: ``` return static_cast <const Target *> (static_cast <const void *> (pointer_to_source)) ``` or: ``` const void *const intermediary (pointer_to_source); // Not `static` for the general case. return static_cast <const Target *> (intermediary); ``` Therefore, if the authors kindly agree, we can make this code an example for this style.
In the C++ Standard Library, `std::integral_constant` has its `operator T` marked as `noexcept`: https://timsong-cpp.github.io/cppwp/meta.help We hereby do the same for the Boost version.
In the C++ Standard Library, `std::integral_constant` has `operator()`: https://timsong-cpp.github.io/cppwp/meta.help We hereby add `operator()` for the Boost version.
|
"I love it when a plan comes together !!"... |
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.
Refs #181