]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
denc: support non-c++20 compliant C++ standard library 47924/head
authorKefu Chai <tchaikov@gmail.com>
Fri, 2 Sep 2022 16:21:57 +0000 (00:21 +0800)
committerKefu Chai <tchaikov@gmail.com>
Sat, 3 Sep 2022 00:24:06 +0000 (08:24 +0800)
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<uint64_t>(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 <tchaikov@gmail.com>
src/include/cpp_lib_backport.h [new file with mode: 0644]
src/include/denc.h

diff --git a/src/include/cpp_lib_backport.h b/src/include/cpp_lib_backport.h
new file mode 100644 (file)
index 0000000..ea956c4
--- /dev/null
@@ -0,0 +1,30 @@
+#pragma once
+
+#include <cstring>
+#include <type_traits>
+
+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<typename To, typename From>
+requires (sizeof(To) == sizeof(From)) &&
+         std::is_trivially_copyable_v<From> &&
+         std::is_trivially_copyable_v<To>
+[[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 to;
+  std::memcpy(&to, &from, sizeof(To));
+  return to;
+#endif
+}
+
+#endif // __cpp_lib_bit_cast
+
+} // namespace std
index 0f080abc678a06a3c505517c9d8f3594d763890c..d075dd518318567667358024c302b44dd77c384b 100644 (file)
@@ -41,6 +41,7 @@
 #include <boost/intrusive/set.hpp>
 #include <boost/optional.hpp>
 
+#include "include/cpp_lib_backport.h"
 #include "include/compat.h"
 #include "include/int_types.h"
 #include "include/scope_guard.h"