mirror of
https://github.com/pytorch/pytorch.git
synced 2025-10-20 21:14:14 +08:00
This PR continues to fix clang-tidy warnings for headers in c10/core and c10/util. Pull Request resolved: https://github.com/pytorch/pytorch/pull/115354 Approved by: https://github.com/Skylion007
32 lines
821 B
C++
32 lines
821 B
C++
#pragma once
|
|
|
|
#include <cstring>
|
|
#include <type_traits>
|
|
|
|
namespace c10 {
|
|
|
|
// Implementations of std::bit_cast() from C++ 20.
|
|
//
|
|
// This is a less sketchy version of reinterpret_cast.
|
|
//
|
|
// See https://en.cppreference.com/w/cpp/numeric/bit_cast for more
|
|
// information as well as the source of our implementations.
|
|
template <class To, class From>
|
|
std::enable_if_t<
|
|
sizeof(To) == sizeof(From) && std::is_trivially_copyable_v<From> &&
|
|
std::is_trivially_copyable_v<To>,
|
|
To>
|
|
// constexpr support needs compiler magic
|
|
bit_cast(const From& src) noexcept {
|
|
static_assert(
|
|
std::is_trivially_constructible_v<To>,
|
|
"This implementation additionally requires "
|
|
"destination type to be trivially constructible");
|
|
|
|
To dst;
|
|
std::memcpy(&dst, &src, sizeof(To));
|
|
return dst;
|
|
}
|
|
|
|
} // namespace c10
|