1 // Copyright (c) 2011-present, Facebook, Inc. All rights reserved. 2 // This source code is licensed under both the GPLv2 (found in the 3 // COPYING file in the root directory) and Apache 2.0 License 4 // (found in the LICENSE.Apache file in the root directory). 5 6 #pragma once 7 8 #include <folly/Traits.h> 9 10 #include <cstdint> 11 #include <cstring> 12 #include <type_traits> 13 14 namespace folly { 15 16 template < 17 typename To, 18 typename From, 19 _t<std::enable_if< 20 sizeof(From) == sizeof(To) && std::is_trivial<To>::value && 21 is_trivially_copyable<From>::value, 22 int>> = 0> bit_cast(const From & src)23To bit_cast(const From& src) noexcept { 24 To to; 25 std::memcpy(&to, &src, sizeof(From)); 26 return to; 27 } 28 29 } // namespace folly 30 31