Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-09-02 23:59:54

0001 #ifndef FWCore_Utilities_bit_cast_h
0002 #define FWCore_Utilities_bit_cast_h
0003 // -*- C++ -*-
0004 //
0005 // Package:     FWCore/Utilities
0006 // Class  :     bit_cast
0007 //
0008 /**\function edm::bit_cast bit_cast.h "FWCore/Utilities/interface/bit_cast.h"
0009 
0010  Description: C++ 20 std::bit_cast stand-in
0011 
0012  Usage:
0013     See documentation on std::bit_cast in C++ 20
0014 
0015 */
0016 //
0017 // Original Author:  Christopher Jones
0018 //         Created:  Wed, 01 Sep 2021 19:11:41 GMT
0019 //
0020 
0021 // system include files
0022 #include <cstring>
0023 
0024 // user include files
0025 
0026 namespace edm {
0027   //in C++20 we can use std::bit_cast which is constexpr
0028   template <class To, class From>
0029   inline To bit_cast(const From &src) noexcept {
0030     static_assert(sizeof(To) == sizeof(From), "incompatible types");
0031     To dst;
0032     std::memcpy(&dst, &src, sizeof(To));
0033     return dst;
0034   }
0035 }  // namespace edm
0036 #endif