Skip to content

Commit

Permalink
[hive] Eliminate bit_cast dependency
Browse files Browse the repository at this point in the history
I don't think it was ever correct to use std::bit_cast here.
Prefer the core-language facility (reinterpret_cast), given that
it does the same thing.
  • Loading branch information
Quuxplusone committed Oct 19, 2023
1 parent 260a3db commit a23e5b1
Showing 1 changed file with 4 additions and 7 deletions.
11 changes: 4 additions & 7 deletions include/sg14/hive.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@
#include <utility>

#if __cplusplus >= 202002L
#include <bit>
#include <compare>
#include <concepts>
#include <ranges>
Expand Down Expand Up @@ -178,15 +177,13 @@ class hive {
NextAndPrev s_;
T t_;
};
PtrOf<T> t() { return bitcast_pointer<PtrOf<T>>(std::addressof(t_)); }
PtrOf<T> t() { return cast_pointer<PtrOf<T>>(std::addressof(t_)); }
~overaligned_elt() = delete;
};

template <class D, class S>
static constexpr D bitcast_pointer(S source_pointer) {
#if __cpp_lib_bit_cast >= 201806L && __cpp_lib_to_address >= 201711L
return std::bit_cast<D>(std::to_address(source_pointer));
#elif __cpp_lib_to_address >= 201711L
static D cast_pointer(S source_pointer) {
#if __cpp_lib_to_address >= 201711L
return reinterpret_cast<D>(std::to_address(source_pointer));
#else
return reinterpret_cast<D>(source_pointer); // reject fancy pointer types
Expand Down Expand Up @@ -1135,7 +1132,7 @@ class hive {
size_t n = (bytes_for_group + bytes_for_elts + bytes_for_skipfield + sizeof(type) - 1) / sizeof(type);
PtrOf<type> p = std::allocator_traits<AllocOf<type>>::allocate(ta, n);
GroupPtr g = PtrOf<group>(p);
::new (bitcast_pointer<void*>(g)) group(cap);
::new (cast_pointer<void*>(g)) group(cap);
return g;
}
static void deallocate_group(allocator_type a, GroupPtr g) {
Expand Down

0 comments on commit a23e5b1

Please sign in to comment.