CMS 3D CMS Logo

bit_cast.h
Go to the documentation of this file.
1 #ifndef FWCore_Utilities_bit_cast_h
2 #define FWCore_Utilities_bit_cast_h
3 // -*- C++ -*-
4 //
5 // Package: FWCore/Utilities
6 // Class : bit_cast
7 //
16 //
17 // Original Author: Christopher Jones
18 // Created: Wed, 01 Sep 2021 19:11:41 GMT
19 //
20 
21 // for compilers that do not support __has_builtin
22 #ifndef __has_builtin
23 #define __has_builtin(x) 0
24 #endif
25 
26 // system include files
27 #include <cstring>
28 #include <type_traits>
29 
30 #if __cplusplus >= 202002L
31 
32 // in C++20 we can use std::bit_cast
33 
34 #include <bit>
35 
36 namespace edm {
37  using std::bit_cast;
38 } // namespace edm
39 
40 #elif __has_builtin(__builtin_bit_cast)
41 
42 // before C++20 we can use __builtin_bit_cast, if supported
43 
44 namespace edm {
45  template <typename To, typename From>
46  constexpr inline To bit_cast(const From &src) noexcept {
47  static_assert(std::is_trivially_copyable_v<From>);
48  static_assert(std::is_trivially_copyable_v<To>);
49  static_assert(sizeof(To) == sizeof(From), "incompatible types");
50  return __builtin_bit_cast(To, src);
51  }
52 } // namespace edm
53 
54 #else
55 
56 #error constexpr edm::bit_cast is not supported by the compiler
57 
58 #endif // __cplusplus >= 202002L
59 
60 #endif // FWCore_Utilities_bit_cast_h
HLT enums.