From 6cc31cc59386d2f38e1af5ee188fa37caab7cd12 Mon Sep 17 00:00:00 2001 From: Kefu Chai Date: Sat, 3 Sep 2022 00:21:57 +0800 Subject: [PATCH] denc: support non-c++20 compliant C++ standard library when compiling with the standard library comes with GCC-10, we have FTBFS like: ``` src/include/denc.h:517:49: error: 'bit_cast' is not a member of 'std'; did you mean 'bad_cast'? 517 | unsigned lowznib = v ? (std::countr_zero(std::bit_cast(v)) / 4) : 0u; | ^~~~~~~~ | bad_cast ``` to address this issue, an implementation of std::bit_cast<> is defined if it is not available. in the long run, we should use a better C++ compiler for compiling the tree. Fixes: https://tracker.ceph.com/issues/57355 Signed-off-by: Kefu Chai --- src/include/cpp_lib_backport.h | 30 ++++++++++++++++++++++++++++++ src/include/denc.h | 1 + 2 files changed, 31 insertions(+) create mode 100644 src/include/cpp_lib_backport.h diff --git a/src/include/cpp_lib_backport.h b/src/include/cpp_lib_backport.h new file mode 100644 index 00000000000..ea956c44605 --- /dev/null +++ b/src/include/cpp_lib_backport.h @@ -0,0 +1,30 @@ +#pragma once + +#include +#include + +namespace std { + +#ifndef __cpp_lib_bit_cast +#define __cpp_lib_bit_cast 201806L + +/// Create a value of type `To` from the bits of `from`. +template +requires (sizeof(To) == sizeof(From)) && + std::is_trivially_copyable_v && + std::is_trivially_copyable_v +[[nodiscard]] constexpr To +bit_cast(const From& from) noexcept { +#if __has_builtin(__builtin_bit_cast) + return __builtin_bit_cast(To, from); +#else + static_assert(std::is_trivially_constructible_v); + To to; + std::memcpy(&to, &from, sizeof(To)); + return to; +#endif +} + +#endif // __cpp_lib_bit_cast + +} // namespace std diff --git a/src/include/denc.h b/src/include/denc.h index 0f080abc678..d075dd51831 100644 --- a/src/include/denc.h +++ b/src/include/denc.h @@ -41,6 +41,7 @@ #include #include +#include "include/cpp_lib_backport.h" #include "include/compat.h" #include "include/int_types.h" #include "include/scope_guard.h" -- 2.39.5