Skip to content

Conversation

@jzmaddock
Copy link
Collaborator

Refs #181

Adder and others added 5 commits April 21, 2025 12:26
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.
@jzmaddock jzmaddock merged commit 085c27f into develop Apr 21, 2025
109 checks passed
@DoctorNoobingstoneIPresume

"I love it when a plan comes together !!"...
Thank you so much for everything !! >:D<

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants