Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Match std::integral_constant by adding operator() and by marking operator T as noexcept. Other minor changes. #181

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from

Conversation

DoctorNoobingstoneIPresume

Conversion between pointers of unrelated types Source and Target can be done with static_cast => IMO 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));

IMO reinterpret_cast should be used for pointer-to-integral and integral-to-pointer conversions (only).

Adder added 3 commits January 6, 2025 23:54
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.
@DoctorNoobingstoneIPresume DoctorNoobingstoneIPresume force-pushed the 230613_integral_constant_reinterpret_cast_01h branch from 510eec1 to 09d7c77 Compare January 6, 2025 22:46
@DoctorNoobingstoneIPresume DoctorNoobingstoneIPresume changed the title Replace reinterpret_cast with static_cast. Match std::integral_constant by adding operator() and by marking operator T as noexcept. Other minor changes. Jan 6, 2025
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.
@DoctorNoobingstoneIPresume DoctorNoobingstoneIPresume force-pushed the 230613_integral_constant_reinterpret_cast_01h branch from 09d7c77 to 633fdb7 Compare January 6, 2025 22:55
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.

1 participant